【问题标题】:Rspec setup issuesRspec 设置问题
【发布时间】: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)>'

Gist for Gemfile

Pastie for Spec Helper

我正在做 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


【解决方案1】:

您需要在test 组中添加shoulda-matchers

group :test do
  gem 'shoulda-matchers'
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多