Is there a better way to check for the existence of a record in RSpec?
Foo.where(bar: 1, baz:2).count.should == 1
Running tests by their file or directory names is the most familiar way to run tests with RSpec. RSpec can take a file name or directory name and run the file or the contents of the directory. So you can do: rspec spec/jobs to run the tests found in the jobs directory.
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-expectations ships with a number of built-in matchers. Each matcher can be used. with expect(..). to or expect(..). not_to to define positive and negative expectations.
The word describe is an RSpec keyword. It is used to define an “Example Group”. You can think of an “Example Group” as a collection of tests. The describe keyword can take a class name and/or string argument.
With Rspec 2.13.0, I was able to do
Foo.where(bar: 1, baz: 2).should exist
Edit:
Rspec now has an expect syntax:
expect(Foo.where(bar: 1, bax: 2)).to exist
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