应该在 Rails 3 中进行测试:应该 respond_with :success

发布于 2024-10-15 05:22:17 字数 1780 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请远离我 2024-10-22 05:22:17

这两个 should 块是单独运行的,并且不会对 respond_with 发出请求,因此会出现错误。您需要在设置块中发出请求,例如:

context "GET to :blame" do
  setup do
    get :blame, :id => @song.id
  end
  should "mark a song as blamed" do
    assert_equal Blame.count, 1
  end
  should respond_with :success
end

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:

context "GET to :blame" do
  setup do
    get :blame, :id => @song.id
  end
  should "mark a song as blamed" do
    assert_equal Blame.count, 1
  end
  should respond_with :success
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文