Ruby Rails:RSpec 无法使用验证
Ruby on Rails: RSpec not working with validation
我有一个相当简单的模型,但是我遇到了 RSpec 个我无法找到解决方案的失败。
型号-
store_accessor :properties, :fields
store_accessor :priorities
belongs_to :user
belongs_to :external_service
validates :user, :external_service, presence: true
validate :service_enabled?
validates_presence_of :properties, message => "This field is non editable"
Rspec -
require 'rails_helper'
module Application
describe Integration, type: :model do
it { should validate_presence_of(:user) }
it { should validate_presence_of(:external_service) }
it { should validate_presence_of(:properties) }
context 'external service' do
let(:service) { Application::ExternalService.new }
before do
allow(subject).to receive(:external_service).and_return(service)
end
end
end
end
这是我遇到的失败:
失败-
Application::Integration should require properties to be set
Failure/Error: it { should validate_presence_of(:properties) }
Expected errors to include "can't be blank" when properties is set to nil,
got errors:
* "can't be blank" (attribute: user, value: nil)
* "can't be blank" (attribute: external_service, value: nil)
* "The service must be enabled to add an integration." (attribute: external_service, value: nil)
* "This field is non editable" (attribute: properties, value: nil)
默认情况下 validates_presence_of
将错误消息视为 can't be blank
。但是,当您设置自定义验证消息时,您必须使用规范
中的 with_message
来验证相同的消息
it { should validate_presence_of(:properties).with_message('This field is non editable') }
我有一个相当简单的模型,但是我遇到了 RSpec 个我无法找到解决方案的失败。
型号-
store_accessor :properties, :fields
store_accessor :priorities
belongs_to :user
belongs_to :external_service
validates :user, :external_service, presence: true
validate :service_enabled?
validates_presence_of :properties, message => "This field is non editable"
Rspec -
require 'rails_helper'
module Application
describe Integration, type: :model do
it { should validate_presence_of(:user) }
it { should validate_presence_of(:external_service) }
it { should validate_presence_of(:properties) }
context 'external service' do
let(:service) { Application::ExternalService.new }
before do
allow(subject).to receive(:external_service).and_return(service)
end
end
end
end
这是我遇到的失败:
失败-
Application::Integration should require properties to be set
Failure/Error: it { should validate_presence_of(:properties) }
Expected errors to include "can't be blank" when properties is set to nil,
got errors:
* "can't be blank" (attribute: user, value: nil)
* "can't be blank" (attribute: external_service, value: nil)
* "The service must be enabled to add an integration." (attribute: external_service, value: nil)
* "This field is non editable" (attribute: properties, value: nil)
默认情况下 validates_presence_of
将错误消息视为 can't be blank
。但是,当您设置自定义验证消息时,您必须使用规范
with_message
来验证相同的消息
it { should validate_presence_of(:properties).with_message('This field is non editable') }