Adh*_*eeb 1 rspec ruby-on-rails shoulda rspec-rails
我正在使用Ruby on Rails 4.2.0和rspec-rails 3.1.0以及应用匹配器2.7.0
当运行测试我有这个错误
Failure/Error: it { should validates_presence_of :name }
NoMethodError:
undefined method `validates_presence_of' for #<RSpec::ExampleGroups::Profile::Vaidations:0x00000007881768>
# ./spec/models/profile_spec.rb:5:in `block (3 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)
这是我的模特
class Profile < ActiveRecord::Base
belongs_to :user
accepts_nested_attributes_for :user
validates :user, presence: true
validates :username, presence: true, uniqueness: { case_sensitive: false }, length: {maximum: 50}
validates :name, presence: true, length: {maximum: 50}
validates :phone, presence: true, uniqueness: { case_sensitive: false }, length: {maximum: 50}
validates :age, presence: true, length: {maximum: 4}
end
Run Code Online (Sandbox Code Playgroud)
这是我的spec文件
require 'rails_helper'
describe Profile do
describe "vaidations" do
it { should validates_presence_of :name }
it { should validates_presence_of :age }
it { should validates_numericality_of :age }
it { should validates_presence_of :phone }
it { should validates_uniqueness_of :phone }
it { should validates_presence_of :username }
it { should validates_uniqueness_of :username }
end
describe 'association' do
it { should belongs_to :user }
end
end
Run Code Online (Sandbox Code Playgroud)
这是我的gemfile中的测试组
group :test do
gem 'rspec-rails'
gem 'shoulda-matchers', require: false
gem 'factory_girl_rails'
end
Run Code Online (Sandbox Code Playgroud)
我发现在shoulda-matchers repo上有相同的错误,但有不同的版本,它对我不起作用!! 我能做什么?!!
请替换以下代码.
it { should validates_presence_of :name }
Run Code Online (Sandbox Code Playgroud)
同
it { is_expected.to validate_presence_of(:name) }
Run Code Online (Sandbox Code Playgroud)
我希望这会奏效.
| 归档时间: |
|
| 查看次数: |
1998 次 |
| 最近记录: |