gpt4 book ai didi

ruby-on-rails - 应该在 Rails 3 中测试 : should respond_with :success

转载 作者:太空宇宙 更新时间:2023-11-03 16:10:35 24 4
gpt4 key购买 nike

我在 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: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'

我使用的是 Ruby 1.9.2、Rails 3.0.3 和 Shoulda 2.11.3。希望有人能帮助我。

谢谢,晚礼服

最佳答案

这两个 should block 是分开运行的,不会对 respond_with 发出任何请求,因此会出现错误。您需要在设置 block 中发出请求,例如:

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

关于ruby-on-rails - 应该在 Rails 3 中测试 : should respond_with :success,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4866045/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com