alp*_*aca 1 ruby rspec ruby-on-rails
运行 rspec 测试时出现如下错误。
Failure/Error: it { should belong_to :user }
NoMethodError:
undefined method `belong_to' for #<RSpec::ExampleGroups::UserEventFavorite:0x007fd0fa9c66c0>
Run Code Online (Sandbox Code Playgroud)
我已经确定belongs_to 特定于Shoulda Matcher。不过,我应该有正确安装早该匹配器为可以从看到spec_helper.rb和rails_helper.rb下面。有人可以告诉我我在这里缺少什么吗?
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
这是规范代码:
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
Run Code Online (Sandbox Code Playgroud)
我解决了这个问题。
我需要添加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
Run Code Online (Sandbox Code Playgroud)
代替
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1455 次 |
| 最近记录: |