ROR 4.0を使用してAgile WDのアプリに取り組んでいます。
order_controller で注文 receive_date を受け取り、それをチェックする統合テストを行いました。コントローラーが注文を作成し、日付と時刻が同じであることをテストで確認します。そしてなぜか1日と3時間の差?両方のケースで同じ関数を使用して日付を取得する方法は、次のとおりです。テストは次のとおりで、その後にコントローラーとテスト出力が続きます。
テスト:
test "buying a product" do
LineItem.delete_all
Order.delete_all
ruby_book = products(:ruby)
get"/"
assert_response :success
assert_template "index"
xml_http_request :post, '/line_items', product_id: ruby_book.id
assert_response :success
cart = Cart.find(session[:cart_id])
assert_equal 1, cart.line_items.size
assert_equal ruby_book, cart.line_items[0].product
get"/orders/new"
assert_response :success
assert_template "new"
receive_date_expected = Time.now.to_date
post_via_redirect "/orders",
order: { name: "Dave Thomas",
address: "123 TheStreet",
email: "dave@example.com",
pay_type: "Check"}
assert_response :success
assert_template "index"
cart = Cart.find(session[:cart_id])
assert_equal 0, cart.line_items.size
orders = Order.all
assert_equal 1, orders.size
order = orders[0]
assert_equal "Dave Thomas", order.name
assert_equal "123 TheStreet", order.address
assert_equal "dave@example.com", order.email
assert_equal "Check", order.pay_type
assert_equal receive_date_expected, order.receive_date.to_date
assert_equal 1, order.line_items.size
line_item = order.line_items[0]
assert_equal ruby_book, line_item.product
mail = ActionMailer::Base.deliveries.last
assert_equal ["dave@example.com"], mail.to
assert_equal 'Sergey Sob <j0nybr4v0@gmail.com>', mail[:from].value
assert_equal "Pragmatic Store Order Confirmation", mail.subject
end
コントローラ:
def create
@order = Order.new(params[:order])
@order.add_line_items_from_cart(current_cart)
@order.receive_date = Time.now.to_date
respond_to do |format|
if @order.save
Cart.destroy(session[:cart_id])
session[:cart_id] = nil
OrderNotifier.received(@order).deliver
format.html { redirect_to store_url, notice: 'Thank you for your order' }
format.json { render json: @order, status: :created, location: @order }
else
@cart = current_cart
format.html { render action: "new" }
format.json { render json: @order.errors, status: :unprocessable_entity }
end
end
出力は次のとおりです。
1) Failure:
test_buying_a_product(UserStoriesTest) [c:/Ruby193/work/depot/test/integration/u
ser_stories_test.rb:44]:
<Wed, 30 May 2012> expected but was
<Tue, 29 May 2012>.