我正在做“Agile Web Development with Rails”一书第 13 章的最后一个练习。
以前有一个名为“Order”的模型,我创建了一个迁移以添加一个名为“ship_date”的列:
add_column :orders, :ship_date, :datetime
然后我在respond_to之前的orders_controller的“create”操作中添加了一行,如下所示:
@order.ship_date = Time.now.to_date
然后我像这样修改了测试文件:
ship_date_expected = Time.now.to_date ### new line
post_via_redirect "/orders",
order: { name: "Dave Thomas",
address: "123 The Street",
email: "dave@example.com",
pay_type: "Check",
ship_date: Time.now.to_date } ### new line
### some existing lines
assert_equal ship_date_expected, order.ship_date ### new line
然后我运行集成测试,但遇到这样的失败:
test_buying_a_product(UserStoriesTest)[H:/Sites/rails_projects/depot/test/integration/user_stories_test.rb:56]:<Tue, 22 Jan 2013> expected but was <nil>.
我检查了数据库并确定新列存在。所以我猜问题出在作业上。这应该是一个愚蠢的问题......但我不知道如何解决它。有人可以帮我吗?
谢谢!!!
创建动作:
def create
@order = Order.new(params[:order])
@order.add_line_items_from_cart(current_cart)
@order.ship_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: I18n.t('.thanks') }
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
end