我使用ROR 4.0从Agile WD开发应用程序。
我的order_controller中有一个订单receive_date,还有一个检查它的集成测试。 Controller创建一个订单,测试检查日期和时间是否相同。由于某种原因,差异是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>.
答案 0 :(得分:1)
尝试将它们比作UTC。在他们上面调用#utc
,然后检查。有什么区别?请务必确保您可以随时拨打#zone
并查看它们是否匹配。
答案 1 :(得分:0)
这似乎是某种时区问题。
尝试在Time.now
处替换Time.zone.now
,这可能有所帮助。