NoMethodError:在Rspec中

时间:2015-09-02 21:55:37

标签: ruby-on-rails ruby rspec

我目前正在对两个文件进行rspec测试,并且我在未定义的方法上遇到了这些失败。我需要更多关于如何准确修复这些错误的钙化,谢谢!

故障:

  1) FavoritesController#create creates a favorite for the current user and specified post
     Failure/Error: @post = create(:post)
     NoMethodError:
       undefined method `create' for #<RSpec::ExampleGroups::FavoritesController::Create:0x007fdabc84f7f8>
     # /Users/bryanporras/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_dispatch/testing/assertions/routing.rb:171:in `method_missing'
     # ./spec/controllers/favorites_controller_spec.rb:8:in `block (2 levels) in <top (required)>'

  2) FavoritesController#destroy destroys the favorite for the current user and post
     Failure/Error: @post = create(:post)
     NoMethodError:
       undefined method `create' for #<RSpec::ExampleGroups::FavoritesController::Destroy:0x007fdabfe0f3e8>
     # /Users/bryanporras/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_dispatch/testing/assertions/routing.rb:171:in `method_missing'
     # ./spec/controllers/favorites_controller_spec.rb:8:in `block (2 levels) in <top (required)>'

  3) VotesController#up_vote adds an up-vote to the post
     Failure/Error: sign_in @user
     NoMethodError:
       undefined method `sign_in' for #<RSpec::ExampleGroups::VotesController::UpVote:0x007fdabfe26fe8>
     # /Users/bryanporras/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_dispatch/testing/assertions/routing.rb:171:in `method_missing'
     # ./spec/controllers/votes_controller_spec.rb:12:in `block (3 levels) in <top (required)>'

  4) Vote after_save calls `Post#update_rank` after save
     Failure/Error: vote = Vote.new(value: 1, post: post)
     NameError:
       undefined local variable or method `post' for #<RSpec::ExampleGroups::Vote::AfterSave:0x007fdabdb8b0b0>
     # ./spec/models/vote_spec.rb:25:in `block (3 levels) in <top (required)>'

Finished in 0.78639 seconds (files took 2.82 seconds to load)
15 examples, 4 failures, 2 pending

Failed examples:

rspec ./spec/controllers/favorites_controller_spec.rb:14 # FavoritesController#create creates a favorite for the current user and specified post
rspec ./spec/controllers/favorites_controller_spec.rb:24 # FavoritesController#destroy destroys the favorite for the current user and post
rspec ./spec/controllers/votes_controller_spec.rb:8 # VotesController#up_vote adds an up-vote to the post
rspec ./spec/models/vote_spec.rb:24 # Vote after_save calls `Post#update_rank` after save

这是favorites_controller_spec.rb文件:

require 'rails_helper'

describe FavoritesController do

  include Devise::TestHelpers

  before do 
    @post = create(:post)
    @user = create(:user)
    sign_in @user
  end

  describe '#create' do
    it "creates a favorite for the current user and specified post" do
      expect( @user.favorites.find_by(post_id: @post.id) ).to be_nil

      post :create, { post_id: @post.id }

      expect( @user.favorites.find_by(post_id: @post.id) ).not_to be_nil
    end
  end

  describe '#destroy' do
    it "destroys the favorite for the current user and post" do
      favorite = @user.favorites.where(post: @post).create
      expect( @user.favorites.find_by(post_id: @post.id) ).not_to be_nil

      delete :destroy, { post_id: @post.id, id: favorite.id }

      expect( @user.favorites.find_by(post_id: @post.id) ).to be_nil
    end
  end
end

这是votes_controller_spec.rb文件:

require 'rails_helper'

 describe VotesController do

   include TestFactories

   describe '#up_vote' do
     it "adds an up-vote to the post" do
        request.env["HTTP_REFERER"] = '/'
       @user = authenticated_user
       @post = associated_post
       sign_in @user

       expect {
         post( :up_vote, post_id: @post.id )
       }.to change{ @post.up_votes }.by 1
     end
   end
 end

1 个答案:

答案 0 :(得分:1)

  1. 检查config.include FactoryGirl::Syntax::Methods
  2. 内是否有rails_helper.rb
  3. 如果您在rails_helper.rb文件中有以上行,则可以始终使用FactoryGirl.create :user
  4. 您可能不会在include Devise::TestHelpers的规范中包含VotesController,这就是为什么它没有看到sign_in方法