我仍然很困惑什么是魔法背后的东西it { should have(1).error_on(:base) },什么是特定的Shoulda匹配器.
我想确保:base包含错误消息"xxx",那么我该怎么做呢?
it "should contain error message 'xxx'" do
contact.valid?
contact.errors[:base].should include('xxx')
end
Run Code Online (Sandbox Code Playgroud)
这是"走的路",还是更好的?谢谢.
我一直在努力奋斗几个小时,我无法在任何地方找到答案.
基本上我正在使用shoulda为我的模型编写规范,并且由于某种原因即使我的let工作正常并且实际应用程序工作正常(手动确认并使用请求规范),如果我包括:
it { expect(user).to validate_uniqueness_of :email }
Run Code Online (Sandbox Code Playgroud)
在我的describe块中,我将收到以下错误:
1) User attributes should require case sensitive unique value for email
Failure/Error: it { expect(user).to validate_uniqueness_of :email }
RuntimeError:
Password digest missing on new record
# ./spec/models/user_spec.rb:14:in `block (3 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)
我已经到了解决这个规范的问题让我退缩,这从来都不是一件好事,因为实施已经有效.
如果有人可以帮助我在这里会很棒,因为我真的不想开始跳过那些因为某些模糊的原因而失败的测试.
我觉得好像rspec'忽略'调用has_secure_password来保存摘要的before方法,但我不确定.我只是假设,因为如果请求规范工作正常,这是做我不知道的事情.
我正在使用rails 4.0.0
以下是相关的代码.我感谢任何帮助.谢谢
user_spec.rb
require 'spec_helper'
describe User do
let(:user) {
user = User.new(:email => 'example@example.com',
:username => 'theo',
:password => 'secretpass',
:password_confirmation => 'secretpass')}
describe "attributes" do
it { expect(user).to validate_presence_of :email }
it …Run Code Online (Sandbox Code Playgroud) 当validate_presence_of使用 Shoulda / RSpec测试多个属性时,我得到这些长而重复的代码块,如下所示:
it { should validate_presence_of(:text) }
it { should validate_presence_of(:user) }
it { should validate_presence_of(:commentable) }
[...]
Run Code Online (Sandbox Code Playgroud)
有没有办法把它弄干?像这样的东西:
it { should validate_presence_of(:text, :user, :commentable,...) }
Run Code Online (Sandbox Code Playgroud) 我使用带有speca-matcher的rspec-rails来测试我的模型。这是代码:
user_ticket.rb
class UserTicket < ActiveRecord::Base
belongs_to :user
belongs_to :ticket
enum relation_type: %w( creator supporter )
validates_uniqueness_of :relation_type, scope: [:user_id, :ticket_id]
end
Run Code Online (Sandbox Code Playgroud)
user_ticket_spec.rb
RSpec.describe UserTicket, type: :model do
subject { FactoryGirl.build(:user_ticket) }
describe 'Relations' do
it { should belong_to(:user) }
it { should belong_to(:ticket) }
end
describe 'Validations' do
it { should define_enum_for(:relation_type).with(%w( creator supporter )) }
# PROBLEM HERE
it { should validate_uniqueness_of(:relation_type).case_insensitive.scoped_to([:user_id, :ticket_id]) }
end
end
Run Code Online (Sandbox Code Playgroud)
当我运行测试用例时,结果始终是:
Failure/Error: it { should validate_uniqueness_of(:relation_type).case_insensitive.scoped_to([:user_id, :ticket_id]) }
ArgumentError:
'CREATOR' is not …Run Code Online (Sandbox Code Playgroud)