How do I write a spec to check if a new record is created in the database after a successful user signup? Using rspec in Rails 4 with capybara, factorygirl, and database_cleaner gems.
I feel like this situation is common and an answer should be easy to find, but I haven't been able to find one here or via google.
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.
let generates a method whose return value is memoized after the first call. This is known as lazy loading because the value is not loaded into memory until the method is called. Here is an example of how let is used within an RSpec test. let will generate a method called thing which returns a new instance of Thing .
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.
You probably want the change matcher
You will do something like:
expect { post :create, :user => {:user => :attributes } }.to change { User.count }
or
expect { post :create, :user => {:user => :attributes } }.to change(User, :count)
This code says:
expect that running this first block of code to change the value I get when I run that block of code
and is functionally equivalent to writing:
before_count = User.count post :create, :user => {:user => :attributes } expect(User.count).not_to eq(before_count)
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