Skip to Content
Rails 5 Test Prescriptions
book

Rails 5 Test Prescriptions

by Noel Rappin
February 2018
Intermediate to advanced
406 pages
9h 52m
English
Pragmatic Bookshelf
Content preview from Rails 5 Test Prescriptions

What’s the Problem?

What’s the big deal if I want to use normal, ordinary ActiveRecord#create in my tests? I use it in my code. What could go wrong?

Since you asked …

I’ll start with a simple test involving two users:

 it ​"can tell which user is older"​ ​do
  eldest_user = User.create(​date_of_birth: ​​'1971-01-22'​)
  youngest_user = User.create(​date_of_birth: ​​'1973-08-31'​)
  expect(User.eldest).to eq(eldest_user)
  expect(User.youngest).to eq(youngest_user)
 end

That test is deliberately simple so as not to distract from the data-creation issue. The only weird thing here is that I’m testing a hypothetical finder method, eldest, that goes into the database, so for the test it’s necessary that the objects I create come from the database. ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Rails 4 Test Prescriptions

Rails 4 Test Prescriptions

Noel Rappin
What Employees Want Most in Uncertain Times

What Employees Want Most in Uncertain Times

Kristine W. Powers, Jessica B.B. Diaz
Agile Web Development with Rails 5.1

Agile Web Development with Rails 5.1

Sam Ruby, David B. Copeland, Dave Thomas

Publisher Resources

ISBN: 9781680505566Errata Page