I have an odd situation whereby if I run an individual rspec model spec file all examples are green, if I test my entire spec/models folder all my examples are green. If I test the controllers they all pass green. If I test the entire suite (via rspec spec) then I get failures - If I remove the controller tests entirely everything is green. Now I'm expecting this is entirely self inflicted but I just can't figure it out.
I've narrowed it down to specific examples in the controller tests - which cause the examples in model specs to fail.
eg. in a notes_controller_spec.rb if this line is present
Note.any_instance.stubs(:valid?).returns(false)
it causes a failure in my models/account_spec.rb
Failure/Error: @account.all_notes.should have(2).notes
ArgumentError:
comparison of Note with Note failed
./app/models/account.rb:293:in `sort'
where line 293 is;
(self.notes + self.transactions.map(&:notes).flatten).sort {|a,b| a.created_at <=> b.created_at }
I'm pretty sure this is going to be one of those face palm moments so be gentle with me!
rspec is a BDD test suite. BDD is short for “Behavior Driven Development,” which means that when you're writing the test for yourself as a developer, you also take into account the behavior that's expected of the user.
To run a single Rspec test file, you can do: rspec spec/models/your_spec. rb to run the tests in the your_spec. rb file.
I use the database_cleaner gem to scrub my test database before each test runs, ensuring a clean slate and stable baseline every time. By default, RSpec will actually do this for you, running every test with a database transaction and then rolling back that transaction after it finishes.
RSpec is a testing tool for Ruby, created for behavior-driven development (BDD). It is the most frequently used testing library for Ruby in production applications. Even though it has a very rich and powerful DSL (domain-specific language), at its core it is a simple tool which you can start using rather quickly.
Are you doing any date setup in a before :all block? These are not transactional and can cause test pollution issues.
Also, I think your syntax might be off here:
Note.any_instance.stubs(:valid?).returns(false)
Should be:
Note.any_instance.stub(:valid?).and_return(false)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With