Ruby on rails 命名错误:以Rspec为单位

Ruby on rails 命名错误:以Rspec为单位,ruby-on-rails,ruby,rspec,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'

我目前正在两个文件上进行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
这是投票_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
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
  • 检查您是否有
    config.include FactoryGirl::Syntax::Methods
    内部
    rails\u helper.rb
  • 如果您在
    rails\u helper.rb
    文件中有上述行,则始终可以使用
    FactoryGirl.create:user
  • 您可能没有将
    include designe::TestHelpers
    包含在
    VotesController
    的规范中,这就是为什么它在
    方法中没有看到
    签名的原因

  • 你弄明白了吗?我得到了相同的错误,但只有当我显式运行有问题的文件时——运行所有规范不会引发此错误