“创建”功能测试失败;使用should_redirect_to
我在尝试将“shoulda”与“factory_girl”一起使用来为 Rails 应用程序中的“create”创建功能测试时遇到问题。我创建了一个简单的项目,脚手架用户,在 test_helper.rb 中添加了“shoulda”(我的系统上当前的 gem 版本 2.11.3 )和“factory_girl”。手动创建用户效果很好。以下是重现失败的步骤:
- rails项目
- 脚手架用户名:字符串
添加在test_helper.rb中:
<块引用>需要“应该” 需要“工厂女孩”
rake db:migrate
为用户编写以下功能测试(覆盖 users_controller_test.rb ):
class UsersControllerTest <;动作控制器::测试用例 Factory.define(:user) 做 |u| u.name '乔' 结尾 上下文“应该创建用户”做 上下文“有效数据”做 设置做 User.any_instance.expects(:save).returns(true).once User.any_instance.stubs(:id).returns(1001) 发布:创建,:用户=> {} 结尾 应该分配给:用户,:类=>用户 should_set_the_flash_to "用户已成功创建。" should_redirect_to("用户页面"){user_path(1001)} 结尾 结尾 结尾
使用“rake test:functions”运行测试显示失败:
预期响应是重定向到
,但 重定向到
。
我还玩过“should redirect_to”,因为我看到“should_redirect_to”已被弃用, 但没有运气。你有什么想法吗?
预先感谢您,
玛丽安·瓦西里·卡莱曼。
I have a problem trying to use 'shoulda' with 'factory_girl' for creating a functional test for 'create' in a Rails application. I created a simple project, scaffolded user, added 'shoulda' (current gem version on my system 2.11.3 ) and 'factory_girl' in test_helper.rb. Creating the user manually works fine. Following are the steps to reproduce the failure :
- rails project
- scaffold user name:string
add in test_helper.rb :
require 'shoulda' require 'factory_girl'
rake db:migrate
write the following functional test for user (override users_controller_test.rb ) :
class UsersControllerTest < ActionController::TestCase Factory.define(:user) do |u| u.name 'joe' end context "should create user" do context "with valid data" do setup do User.any_instance.expects(:save).returns(true).once User.any_instance.stubs(:id).returns(1001) post :create, :user => {} end should_assign_to :user, :class => User should_set_the_flash_to "User was successfully created." should_redirect_to("user page"){user_path(1001)} end end end
Running the test with "rake test:functionals" shows failure :
Expected response to be a redirect to
<http://test.host/users/1001>
but was
a redirect to<http://test.host/users>
.
I played also with "should redirect_to", because I saw "should_redirect_to" is deprecated,
but with no luck. Do you have any ideas ?
Thank you in advance,
Marian Vasile Caraiman.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要存根
User#id
,而是存根User.create
并使用特定的用户模拟对象。Instead of stubbing
User#id
, stubUser.create
and use particular user mock object.