我正在使用Rails 3中的paper_trail v2.6.3通过遵循小径来尝试一个示例。我想为一个模型编写一个规范,让我检查它在paper_trail下是否有版本,比如:
it { should be_trailed }be_trailed应该是一个定制的rspec匹配器,它应该检查模型是否是版本的。
我该怎么写规范?
我不想恢复版本。我只想看看它是否有版本。
我在demo_app上使用它,遵循Michael的Rails教程
class User < ActiveRecord::Base
attr_accessible :email, :name
has_paper_trail
end 发布于 2012-11-10 05:21:53
如果您想知道如何编写RSpec匹配器,文档是这里。
如果您询问matcher应该做什么,可以尝试检查对象是否响应paper_trail提供的方法。例如
RSpec::Matchers.define :be_trailed do
match do |actual|
actual.respond_to?(:versions)
end
end发布于 2014-12-11 02:56:14
共享上下文
我个人喜欢通过Rspec的共享上下文测试我的模型是否包括PaperTrail,如下所示:
./spec/support/shared_contexts/paper_trail_contexts.rb
shared_context 'a PaperTrail model' do
it { should respond_to(:versions) }
# You can add other assertions here as well if you like.
end./spec/model/user_spec.rb
it_behaves_like 'a PaperTrail model'Rspec输出
User
behaves like a PaperTrail model
should respond to #versions我发现这比使用自定义匹配器(如be_trailed )更清晰、更可扩展。
https://stackoverflow.com/questions/13319249
复制相似问题