ruby-on-rails – 将更清晰的描述传递给RSpec`his`方法
发布时间:2020-12-17 02:32:29 所属栏目:百科 来源:网络整理
导读:我是一个RSpec新手,但我真的很喜欢编写测试是多么容易,而且当我学习RSpec的新功能时,我会不断重构它们.所以,最初,我有以下内容: describe Account do context "when new" do let(:account) { Account.new } subject { account } it "should have account at
我是一个RSpec新手,但我真的很喜欢编写测试是多么容易,而且当我学习RSpec的新功能时,我会不断重构它们.所以,最初,我有以下内容:
describe Account do context "when new" do let(:account) { Account.new } subject { account } it "should have account attributes" do subject.account_attributes.should_not be_nil end end end 然后我了解了它的方法,所以我试着重写它: describe Account do context "when new" do let(:account) { Account.new } subject { account } its(:account_attributes,"should not be nil") do should_not be_nil end end end 这是因为它不接受2个参数而失败,但删除消息的工作正常.问题是,如果测试失败,则“失败示例”部分下的消息只会说明 rspec ./spec/models/account_spec.rb:23 # Account when new account_attributes 这不是太有帮助. 那么,有没有办法将消息传递给它,或者更好的是,让它自动输出一个合理的消息? 解决方法
您可以定义RSpec自定义匹配器:
RSpec::Matchers.define :have_account_attributes do match do |actual| actual.account_attributes.should_not be_nil end failure_message_for_should do "expected account_attributes to be present,got nil" end end describe Account do it { should have_account_attributes } end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |