为什么在上下文块中调用“should respond_with:success”时会出现“method_missing”错误?

时间:2011-12-08 03:04:58

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

以下是错误和测试的要点: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

2 个答案:

答案 0 :(得分:0)

尝试包括括号:成功,should respond_with(:success)

答案 1 :(得分:0)

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