在查看有关控制器测试的教程时,作者举了一个例子,测试了一个控制器动作的rspec测试。我的问题是,为什么他们使用attributes_for
方法而不是build
方法?除了它返回值的哈希之外,没有明确的解释为什么要使用attributes_for
。
it "redirects to the home page upon save" do
post :create, contact: Factory.attributes_for(:contact)
response.should redirect_to root_url
end
教程链接在此处:http://everydayrails.com/2012/04/07/testing-series-rspec-controllers.html 示例位于开始主题部分的 Controller testing basics
post :create, user: u.attributes
? - Adam Waite