【发布时间】:2014-03-23 08:55:09
【问题描述】:
我的 rspec 设置有问题,但我不确定是什么问题,所有规范都按正常编写,但没有正常运行。
例如,
13) Event associations should have many :jobs
Failure/Error: it { should have_many :jobs }
NoMethodError:
undefined method `has_many?' for #<Event:0x007ffdb0140978>
# ./spec/models/event_spec.rb:14:in `block (3 levels) in <top (required)>'
8) Event validations
Failure/Error: it { should validate_presence_of :start_date }
NoMethodError:
undefined method `validate_presence_of' for #<RSpec::ExampleGroups::Event_2::Validations:0x007ffdb02a1268>
# ./spec/models/event_spec.rb:7:in `block (3 levels) in <top (required)>'
我正在做 TDD,所以在它启动并运行之前无法真正开始,请提供帮助!
编辑
事件.rb
class Event < ActiveRecord::Base
validates_presence_of :start_date, :end_date, :description, :title
has_many :jobs
has_many :applications, through: :jobs
has_many :users, through: :applications
has_many :companies, through: :jobs
end
require 'spec_helper'
describe Event do
context "validations" do
it { should validate_presence_of :title }
it { should validate_presence_of :description }
it { should validate_presence_of :start_date }
it { should validate_presence_of :end_date }
end
context "associations" do
it { should have_many :companies }
it { should have_many :jobs }
it { should have_many(:applications).through(:jobs) }
it { should have_many(:users).through(:applications) }
end
context "custom methods" do
end
end
【问题讨论】:
-
如果您显示导致错误的代码会更有帮助。即 event_spec.rb
-
@Dty 添加了!看看,谢谢
-
你在使用Shouda吗?如果是这样,请将此 gem 包含在所需的匹配器中:github.com/thoughtbot/shoulda-matchers
标签: ruby-on-rails ruby rspec ruby-on-rails-4