【问题标题】:Why I would be getting a "method_missing" error when calling "should respond_with :success" in context block?为什么在上下文块中调用“should respond_with :success”时会收到“method_missing”错误?
【发布时间】:2011-12-08 03:04:58
【问题描述】:

这是错误和测试的要点:https://gist.github.com/1445801

这里是要点内容:

require 'test_helper'

class Api::HotelsControllerTest < ActionController::TestCase

    context "When not logged in" do
        should "forbid access when creating a hotel" do
            hotel = Factory.build(:hotel)
            post :create, :hotel => hotel
            assert @response.code == "401"
            assert Hotel.count.zero?
        end

        should "forbid access when listing available hotels" do
            get :available
            assert @response.code == "401"
        end

        should "forbid access when listing hotels" do
            get :index
            assert @response.code == "401"
        end

        should "forbid access when showing a hotel" do
            hotel = Factory(:hotel)
            get :show, :id => hotel.id
            assert @response.code == "401"
        end

        should "forbid when updating a hotel" do
            hotel = Factory(:hotel)
            hotel.name = "Some new Hotel name"
            put :update, :id => hotel.id, :hotel => hotel
            assert @response.code == "401"
        end
  end

    context "When logged in as basic user" do
        setup do
            @user = Factory(:user)
            sign_in @user
        end

        context "on GET available hotel with no search criteria" do
            setup do
                get :available
            end

            should respond_with :success
            should "be valid response" do
                true
            end
        end
    end

end

还有测试:

** Invoke test (first_time)
** Execute test
** Invoke test:units (first_time)
** Invoke test:prepare (first_time)
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:abort_if_pending_migrations
** Execute db:test:prepare
** Invoke db:test:load (first_time)
** Invoke db:test:purge (first_time)
** Invoke environment 
** Execute db:test:purge
** Execute db:test:load
** Invoke db:schema:load (first_time)
** Invoke environment 
** Execute db:schema:load
NOTICE:  CREATE TABLE will create implicit sequence "addresses_id_seq" for serial column "addresses.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "addresses_pkey" for table "addresses"
NOTICE:  CREATE TABLE will create implicit sequence "amenities_id_seq" for serial column "amenities.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "amenities_pkey" for table "amenities"
NOTICE:  CREATE TABLE will create implicit sequence "hotels_id_seq" for serial column "hotels.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "hotels_pkey" for table "hotels"
NOTICE:  CREATE TABLE will create implicit sequence "locations_id_seq" for serial column "locations.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "locations_pkey" for table "locations"
NOTICE:  CREATE TABLE will create implicit sequence "photos_id_seq" for serial column "photos.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "photos_pkey" for table "photos"
NOTICE:  CREATE TABLE will create implicit sequence "rooms_id_seq" for serial column "rooms.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "rooms_pkey" for table "rooms"
NOTICE:  CREATE TABLE will create implicit sequence "user_hotel_taggings_id_seq" for serial column "user_hotel_taggings.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "user_hotel_taggings_pkey" for table "user_hotel_taggings"
NOTICE:  CREATE TABLE will create implicit sequence "users_id_seq" for serial column "users.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "users_pkey" for table "users"
** Execute test:prepare
** Execute test:units
** Invoke test:functionals (first_time)
** Invoke test:prepare 
** Execute test:functionals
/Users/Eric/Work/webapp/.bundle/ruby/1.9.1/gems/shoulda-2.11.3/lib/shoulda/context.rb:429:in `method_missing': undefined method `respond_with' for Api::HotelsControllerTest:Class (NoMethodError)
    from /Users/Eric/Work/webapp/test/functional/api/hotels_controller_test.rb:48:in `block (2 levels) in <class:HotelsControllerTest>'
    from /Users/Eric/Work/webapp/.bundle/ruby/1.9.1/gems/shoulda-2.11.3/lib/shoulda/context.rb:306:in `call'
    from /Users/Eric/Work/webapp/.bundle/ruby/1.9.1/gems/shoulda-2.11.3/lib/shoulda/context.rb:306:in `merge_block'
    from /Users/Eric/Work/webapp/.bundle/ruby/1.9.1/gems/shoulda-2.11.3/lib/shoulda/context.rb:301:in `initialize'
    from /Users/Eric/Work/webapp/.bundle/ruby/1.9.1/gems/shoulda-2.11.3/lib/shoulda/context.rb:310:in `new'
    from /Users/Eric/Work/webapp/.bundle/ruby/1.9.1/gems/shoulda-2.11.3/lib/shoulda/context.rb:310:in `context'
    from /Users/Eric/Work/webapp/test/functional/api/hotels_controller_test.rb:43:in `block in <class:HotelsControllerTest>'
    from /Users/Eric/Work/webapp/.bundle/ruby/1.9.1/gems/shoulda-2.11.3/lib/shoulda/context.rb:306:in `call'
    from /Users/Eric/Work/webapp/.bundle/ruby/1.9.1/gems/shoulda-2.11.3/lib/shoulda/context.rb:306:in `merge_block'
    from /Users/Eric/Work/webapp/.bundle/ruby/1.9.1/gems/shoulda-2.11.3/lib/shoulda/context.rb:301:in `initialize'
    from /Users/Eric/Work/webapp/.bundle/ruby/1.9.1/gems/shoulda-2.11.3/lib/shoulda/context.rb:199:in `new'
    from /Users/Eric/Work/webapp/.bundle/ruby/1.9.1/gems/shoulda-2.11.3/lib/shoulda/context.rb:199:in `context'
    from /Users/Eric/Work/webapp/test/functional/api/hotels_controller_test.rb:37:in `<class:HotelsControllerTest>'
    from /Users/Eric/Work/webapp/test/functional/api/hotels_controller_test.rb:3:in `<top (required)>'
    from /Users/Eric/Work/webapp/.bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/rake_test_loader.rb:10:in `require'
    from /Users/Eric/Work/webapp/.bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/rake_test_loader.rb:10:in `block (2 levels) in <main>'
    from /Users/Eric/Work/webapp/.bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/rake_test_loader.rb:9:in `each'
    from /Users/Eric/Work/webapp/.bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/rake_test_loader.rb:9:in `block in <main>'
    from /Users/Eric/Work/webapp/.bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/rake_test_loader.rb:4:in `select'
    from /Users/Eric/Work/webapp/.bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/rake_test_loader.rb:4:in `<main>'
** Invoke test:integration (first_time)
** Invoke test:prepare 
** Execute test:integration
Errors running test:functionals!
rake test TESTOPTS="-v" --trace  14.58s user 1.96s system 89% cpu 18.499 total

【问题讨论】:

  • 你确定你已经安装了 shoulda-matchers gem 并且它在 gemfile 中的 rspec gem 之后吗?
  • 我没有使用 rspec。这是我的 Gemfile gist.github.com/1445981 的内容。
  • 您不需要在 Gemfile 中同时包含 rspec-railsshoulda-matcher 吗? (来自shoulda-matcher gem README.rdoc github.com/thoughtbot/shoulda-matchers
  • @buruzaemon 我不这么认为。我正在根据 ruby​​gems.org (github.com/rubygems/rubygems.org) 的编写方式对我的测试进行建模,它们似乎没有在任何地方引用 rspec。

标签: ruby-on-rails testing functional-testing shoulda


【解决方案1】:

尝试在 :success、should respond_with(:success) 周围加上括号。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题(使用相同版本的 Shoulda -- 2.11.3 -- 以及像你这样的命名空间控制器)并通过将 Shoulda 升级到 3.1.1 来修复它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-22
      • 1970-01-01
      • 2019-06-28
      • 1970-01-01
      • 2016-04-30
      • 1970-01-01
      • 2019-12-24
      相关资源
      最近更新 更多