In the previous article you learnt how to write a Rails action that can handle concurrent users, an asynchronous action. Today you are going to learn how to write an RSpec test for it.
This is the controller we want to write a test for:
If this were not an asynchronous action, we would simply write a test like this:
But this won’t work because throw :async
will kill the action right away. throw :async
will make the interpreter go to this line of code defined in the Thin gem. The problem is that Thin is not loaded at all when running a controller test.
In order to properly test an asynchronous action you need to run the test using a real Thin server. You can use Capybara to do that.
Gemfile
In spec/rails_helper.rb:
Write the test:
And that’s it. Happy testing.
SHARE THIS PAGE!