Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Rails) PaperTrail and RSpec

I'm having trouble with PaperTrail (auto-versioning of objects for Rails) being used with RSpec tests. Normally I want my tests to run without PaperTrail versioning, but there are a handful of tests for which I want PaperTrail turned on. I typically run my tests with Guard and Spork, and I can use things like PaperTrail.enabled = true and PaperTrail.enabled = false around a given test and everything works fine.

However, when I run the tests with RSpec, the tests requiring PaperTrail fail. To be more specific, it appears that while code in before filters can produce versions objects, code in the tests cannot. After a considerable amount of digging and tinkering and trying code snippets (I've tried this and this), it looks like the best solution is to use the require "paper_trail/frameworks/rspec" line mentioned in the PaperTrail README.

Unfortunately, each of these keeps me right where I started—tests pass with Guard/Spork but not vanilla RSpec. This is in particular an issue because while I use Spork locally, our continous integration server runs RSpec directly.

Does anyone have any insight?

like image 901
JacobEvelyn Avatar asked Oct 08 '13 19:10

JacobEvelyn


2 Answers

PaperTrail now has documentation on tests with vanilla rspec

https://github.com/paper-trail-gem/paper_trail#7b-rspec

After including require 'paper_trail/frameworks/rspec' in your spec/rails_helpers.rb

... PaperTrail will be turned off for all tests by default. To enable PaperTrail for a test you can either wrap the test in a with_versioning block, or pass in versioning: true option to a spec block.

like image 131
hkly Avatar answered Sep 30 '22 13:09

hkly


Somehow my issue was fixed by changing before(:all) and after(:all) behavior to before(:each) and after(:each).

like image 26
JacobEvelyn Avatar answered Sep 30 '22 14:09

JacobEvelyn