Using Mocks to Simulate Database Failure
Learn how to simulate database failure using mocks.
We'll cover the following...
Simulating database failure
A common use case for test doubles in Rails is to simulate database failures. Let’s look at how we can do that.
In the past, we would have suggested testing for failure at the controller level. However, Rails has basically deprecated controller tests for reasons we’ll talk about in Testing Rails Display Elements, and so we’d like to stay within the bounds of Rails community behavior.
Let’s look at the project-creation functionality we wrote at the beginning of the book. We wrote a controller:
Press + to interact
def create@workflow = CreatesProject.new(name: params[:project][:name],task_string: params[:project][:tasks])@workflow.createif @workflow.success?redirect_to projects_pathelse@project = @workflow.projectrender :newendend
Workflow
The controller largely defers to a workflow object that does the actual work:
Press + to interact
class CreatesProjectattr_accessor :name, :project, :task_stringdef initialize(name: "", task_string: "")@name = name@task_string = task_string || ""@success = falseenddef success?@successenddef buildself.project = Project.new(name: name)project.tasks = convert_string_to_tasksprojectenddef createbuildresult = project.save@success = resultenddef convert_string_to_taskstask_string.split("\n").map do |one_task|title, size_string = one_task.split(":")Task.new(title: title, size: size_as_integer(size_string))endenddef size_as_integer(size_string)return 1 if size_string.blank?[size_string.to_i, 1].maxendend
Now we have two different pieces of point-of-failure logic to deal with:
-
In the event of a database failure, the ...