应该在 Rails 3 中进行测试:应该 respond_with :success
我在 Rails 3 中测试时遇到一些问题。我目前正在将 Rails2 应用程序升级到 Rails3。我正在使用 shoulda 进行测试。在我的功能测试中,我正在使用 shoulda 进行测试,GET 应该成功响应
context "GET to :blame" do
should "mark a song as blamed" do
get :blame, :id => @song.id
assert_equal Blame.count, 1
get :blame, :id => @song.id
assert_equal Blame.count, 2
end
should respond_with :success
end
在下一行中,我在使用 rake test:functions 执行功能测试时收到以下错误:
1) Error:
test: a visitor GET to :blame should respond with 200. (SongsControllerTest):
NoMethodError: undefined method `response_code' for nil:NilClass
/Users/23tux/.rvm/gems/ruby-1.9.2-p136@rails3/gems/activesupport-3.0.3/lib/active_support/whiny_nil.rb:48:in `method_missing'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136@rails3/gems/shoulda-2.11.3/lib/shoulda/action_controller/matchers/respond_with_matcher.rb:57:in `response_code'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136@rails3/gems/shoulda-2.11.3/lib/shoulda/action_controller/matchers/respond_with_matcher.rb:48:in `correct_status_code?'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136@rails3/gems/shoulda-2.11.3/lib/shoulda/action_controller/matchers/respond_with_matcher.rb:30:in `matches?'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136@rails3/gems/shoulda-2.11.3/lib/shoulda/assertions.rb:53:in `assert_accepts'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136@rails3/gems/shoulda-2.11.3/lib/shoulda/context.rb:324:in `block in should'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136@rails3/gems/shoulda-2.11.3/lib/shoulda/context.rb:382:in `call'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136@rails3/gems/shoulda-2.11.3/lib/shoulda/context.rb:382:in `block in create_test_from_should_hash'
我正在使用 Ruby 1.9。 2、Rails 3.0.3 和 Shoulda 2.11.3。 希望,有人可以帮助我。
谢谢, 无尾礼服
I have some troubles with testing in Rails 3. I'm currently upgrading a Rails2 app to Rails3. I'm using shoulda for testing. In my functional tests, I'm testing with shoulda, that a GET should respond with success
context "GET to :blame" do
should "mark a song as blamed" do
get :blame, :id => @song.id
assert_equal Blame.count, 1
get :blame, :id => @song.id
assert_equal Blame.count, 2
end
should respond_with :success
end
In the next to last line I'm getting the following error while executing the functional tests with rake test:functionals:
1) Error:
test: a visitor GET to :blame should respond with 200. (SongsControllerTest):
NoMethodError: undefined method `response_code' for nil:NilClass
/Users/23tux/.rvm/gems/ruby-1.9.2-p136@rails3/gems/activesupport-3.0.3/lib/active_support/whiny_nil.rb:48:in `method_missing'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136@rails3/gems/shoulda-2.11.3/lib/shoulda/action_controller/matchers/respond_with_matcher.rb:57:in `response_code'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136@rails3/gems/shoulda-2.11.3/lib/shoulda/action_controller/matchers/respond_with_matcher.rb:48:in `correct_status_code?'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136@rails3/gems/shoulda-2.11.3/lib/shoulda/action_controller/matchers/respond_with_matcher.rb:30:in `matches?'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136@rails3/gems/shoulda-2.11.3/lib/shoulda/assertions.rb:53:in `assert_accepts'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136@rails3/gems/shoulda-2.11.3/lib/shoulda/context.rb:324:in `block in should'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136@rails3/gems/shoulda-2.11.3/lib/shoulda/context.rb:382:in `call'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136@rails3/gems/shoulda-2.11.3/lib/shoulda/context.rb:382:in `block in create_test_from_should_hash'
I'm using Ruby 1.9.2, Rails 3.0.3 and Shoulda 2.11.3.
Hope, someone can help me.
thx,
tux
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这两个 should 块是单独运行的,并且不会对 respond_with 发出请求,因此会出现错误。您需要在设置块中发出请求,例如:
The two should blocks are run separately and no request would be made for respond_with hence the error. You would need to make the request in a setup block such as: