ruby-on-rails - Rspec 设置问题

标签 ruby-on-rails ruby rspec ruby-on-rails-4

我的 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

最佳答案

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

group :test do
  gem 'shoulda-matchers'
end

关于ruby-on-rails - Rspec 设置问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22588805/

相关文章:

sql - ActiveRecord:find_by_sql 并包括

ruby-on-rails - Ruby - 通过循环创建按位置分组的数组数组

ruby-on-rails - 请求规范中的 Rspec 和 Devise

ruby-on-rails - 重定向 404、422、500 Ruby On rails

ruby-on-rails - 使用正则表达式验证日期格式

ruby 信号量?

ruby-on-rails - Rails 应用程序的领域驱动设计 : Implementing a service in a basic example

ruby-on-rails - 为什么这个 Rspec 测试返回 "email already taken"

ruby-on-rails - 使用rspec和pdfkit测试pdf的下载

ruby-on-rails - Carrierwave 上传适用于 Rails 控制台,但不适用于规范