Frage

I am trying an example using paper_trail v2.6.3 in Rails 3 by following the Github Documentation for paper_trail. I want to write a spec for a model that lets me check if its versioned under paper_trail something like:

it { should be_trailed }

and be_trailed should be a custom rspec matcher that should check if the model is versioned or not.

How do I write the spec?

P.S. I do not want to revert versions. I just want to check if its versioned.

I am using it on the demo_app following Michael Hartl's Rails Tutorial:

class User < ActiveRecord::Base
  attr_accessible :email, :name
  has_paper_trail
end 
War es hilfreich?

Lösung

If you're asking how to write RSpec matchers, the documentation is here.

If you're asking what the matcher should do, you could try checking to see if the object responds to the methods provided by paper_trail. For example

RSpec::Matchers.define :be_trailed do
  match do |actual|
    actual.respond_to?(:versions)
  end
end

Andere Tipps

Shared Contexts

I personally like to test that my models are including PaperTrail via Rspec's shared contexts, like so:

./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/models/user_spec.rb

it_behaves_like 'a PaperTrail model'

Rspec output

User
  behaves like a PaperTrail model
    should respond to #versions

I find this clearer and more expandable then using a custom matcher, like be_trailed.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top