我目前正在尝试设置我的测试套件,我似乎不能让应该的助手工作。我以前从来没有亲自设置过这个,但我在一个项目上工作,它已经实现了。我认为一切都是需要的,但显然我遗漏了一些东西。找个人指出我错过了什么。这是我的代码。
模型规范:
require "rails_helper"
describe Subscriber do
context "validations" do
it { is_expected.to validate_presence_of(:first_name) }
it { is_expected.to validate_presence_of(:last_name) }
it { is_expected.to validate_presence_of(:email) }
it { is_expected.to validate_presence_of(:password) }
end
end
模型:
class Subscriber < ActiveRecord::Base
has_many :comments
VALID_EMAIL_REGEX = /A[w+-.]+@[a-zd-]+(.[a-z]+)*.[a-z]+z/i
VALID_PHONE_REGEX = /d{10}/
validates :first_name, presence: true
validates :last_name, presence: true
validates :email,
:presence => true,
:uniqueness => true,
:format => VALID_EMAIL_REGEX
validates :phone_number,
:presence => true,
:uniqueness => true,
:numericality => true,
:format => VALID_PHONE_REGEX
def date_joined
created_at.strftime("%-m/%-d/%-y")
end
def expiration_date
(created_at + 1.year).strftime("%-m/%-d/%-y")
end
def days_till_expired
((created_at + 1.year) - DateTime.now).to_i / 1.day
end
end
GEMFILE:
group :development, :test do
gem 'byebug'
gem "sqlite3"
gem "rspec-rails"
gem "capybara"
gem "factory_girl_rails"
gem "simplecov", require: false
gem "shoulda-matchers", require: false
end
错误:NoMethodError:
undefined method `validate_presence_of' for # <RSpec::ExampleGroups::Subscriber::Validations:0x007fc257ea50d0>
# ./spec/models/subscriber_spec.rb:12:in `block (3 levels) in <top (required)>'
所有四个的错误是相同的。
据我所知,这就是我所需要的。如果您需要查看更多代码,请告诉我。
您是否在您的rails_helper.rb中添加了下列内容?
Shoulda::Matchers.configure do |config|
config.integrate do |with|
# Choose a test framework:
with.test_framework :rspec
# Choose one or more libraries:
with.library :rails
end
end
进一步,为什么你需要false?,你应该从你的gemfile中删除require false。GEMFILE:
group :development, :test do
gem 'byebug'
gem "sqlite3"
gem "rspec-rails"
gem "capybara"
gem "factory_girl_rails"
gem "simplecov", require: false
gem "shoulda-matchers", require: false
end