我正在使用带有 ROR 4.0 的 Agile WD 开发应用程序。
我在 order_controller 中有一个 order 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>.