Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to elegantly check for existence of a record in RSpec

Tags:

ruby

rspec

Is there a better way to check for the existence of a record in RSpec?

Foo.where(bar: 1, baz:2).count.should == 1 
like image 566
B Seven Avatar asked Dec 07 '12 03:12

B Seven


People also ask

How do I run a test in RSpec?

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.

Does RSpec clean database?

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.

What is expect in RSpec?

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.

What is describe in RSpec?

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.


1 Answers

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 
like image 87
codegoalie Avatar answered Sep 29 '22 03:09

codegoalie