Skip to Main Content
Rails 5 Test Prescriptions
book

Rails 5 Test Prescriptions

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

Using Mocks to Simulate Database Failure

A common use case for test doubles in Rails is to simulate database failures. Let’s look at how you might do that.

In the past, I would have suggested testing for failure at the controller level. However, Rails has basically deprecated controller tests for reasons I’ll talk about in Chapter 11, Testing Rails Display Elements, and so I’d like to stay within the bounds of Rails community behavior.

Let’s look at the project-creation functionality you wrote at the beginning of the book. You wrote a controller:

 def​ create
  @workflow = CreatesProject.new(
 name: ​params[​:project​][​:name​],
 task_string: ​params[​:project​][​:tasks​])
  @workflow.create ...
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
Agile Web Development with Rails 5.1

Agile Web Development with Rails 5.1

Sam Ruby, David B. Copeland, Dave Thomas
Rspec Essentials

Rspec Essentials

Mani Tadayon

Publisher Resources

ISBN: 9781680505566Errata Page