2016-12-19 33 views
1

rspecテストを実行する際に次のエラーが発生します。RSpec、Railsのための未定義メソッド `belongs_to '

Failure/Error: it { should belong_to :user } 

    NoMethodError: 
     undefined method `belong_to' for #<RSpec::ExampleGroups::UserEventFavorite:0x007fd0fa9c66c0> 

私は、belongs_toがShoulda Matcherに固有であると特定しました。しかし、下記のspec_helper.rbrails_helper.rbのようにShoulda Matcherを正しくインストールする必要があります。誰かがここで私が逃していることを教えてもらえますか私はこの問題を考え出し

require 'spec_helper' 
require 'rails_helper' 
describe UserEventFavorite do 
    let(:user_event_favorite) { FactoryGirl.build :user_event_favorite } 
    subject { user_event_favorite } 

    it { should respond_to :user_id } 
    it { should respond_to :event_id } 

    it { should belong_to :user } 
    it { should belong_to :event } 
end 
+0

あなたは、スペックコードをしてください追加することはできますか? –

+0

私の悪い!ただ追加されました。 – alpaca

+1

スペルファイルに 'rails_helper''が必要なことを忘れてしまったのでしょうか? –

答えて

2

spec_helper.rb

ENV["RAILS_ENV"] ||= 'test' 
require File.expand_path("../../config/environment", __FILE__) 
require 'rspec/rails' 
require 'shoulda/matchers' 

RSpec.configure do |config| 
    config.expect_with :rspec do |expectations| 
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true 
    end 
    config.mock_with :rspec do |mocks| 
    mocks.verify_partial_doubles = true 
    end 
    config.shared_context_metadata_behavior = :apply_to_host_groups 
    config.include RSpec::RequestDescriber, type: :request 
    config.before :all do 
    FactoryGirl.factories.clear 
    FactoryGirl.reload 
    end 
    config.before :suite do 
    DatabaseRewinder.clean_all 
    end 
    config.after :each do 
    DatabaseRewinder.clean 
    end 
    Autodoc.configuration.toc = true 
end 

rails_helper.rb

RSpec.configure do |config| 
    config.include RSpec::RequestDescriber, type: :request 
    # config.include RequestHelpers, type: :request 
    # config.include RequestMacros, type: :request 

    config.before :all do 
    FactoryGirl.factories.clear 
    FactoryGirl.find_definitions 
    FactoryGirl.reload 
    end 

    config.before :suite do 
    DatabaseRewinder.clean_all 
    end 

    config.after :each do 
    DatabaseRewinder.clean 
    end 

    Shoulda::Matchers.configure do |config| 
    config.integrate do |with| 
     with.test_framework :rspec 
     with.library :rails 
    end 
    end 
end 

はここでスペックコードです。

type: :modelをスペックコードに追加する必要がありました。作業溶液は次のとおりです。

describe UserEventFavorite, type: :model do 
    let(:user_event_favorite) { FactoryGirl.build :user_event_favorite } 
    subject { user_event_favorite } 

    it { should respond_to :user_id } 
    it { should respond_to :event_id } 

    it { should belong_to :user } 
    it { should belong_to :event } 
end 

代わりの

describe UserEventFavorite do 
    let(:user_event_favorite) { FactoryGirl.build :user_event_favorite } 
    subject { user_event_favorite } 

    it { should respond_to :user_id } 
    it { should respond_to :event_id } 

    it { should belong_to :user } 
    it { should belong_to :event } 
end 
関連する問題