Rspec learning resources
The best way to get a grasp of testing in general and rspec is to start from scratch a project and start writing down each test, that way you’ll be able to see what happens if you forget to close either a context
block or an it
block or even more intricate subjects like what’s the difference between describe
and context
.
While developing a freelance project I wanted to add testing as a way to push myself to learn more about testing and also give rspec a try and by doing it I learned how to use gems like: faker, shoulda-matchers, factory_bot and finally rspec. 🎲
You’ll additionally acquire:
– how to read the output of the test suite in the console.
– difference between create
and build
more details.;
# build doesn't persist
user = build(:user, name: "John")
expect(user.name).to eq("John")
# create does persist
user = create(:user, name: "Jane")
expect(user.persisted?).to be true
– How to include Devise’s test helpers
class PostsControllerTest < ActionController::TestCase
config.include Devise::Test::ControllerHelpers, type: :requests
end
blog;
– difference between fixtures
and factories
;
– and finally learn how to integrate FactoryBot with RSpec link.
class Test::Unit::TestCase
include FactoryBot::Syntax::Methods
end
Here are also another resources that I followed in order to cement more my knowledge.
2 hours long video from Type Fast a Youtube channel that makes rails tutorials. The video is very thorough. Covers model, requests, background job testing and system tests. Fairly comprehensive starter guide to RSpec
1 hour long rspec tutorial from Type Fast was well (it’s more on the setup for an app) testing setup with rspec
How to test models CJ Avila
How to use factory_bot Deanin, short tutorial
Full marathon of rspec tutorial (reading not video) from syntax, how to test a model and installing rspec Remi rspec tutorials
The Complete Guide to Rails Testing by Jason Swett link
Learn by doing is the best way to learn rspec DSL, development stack, learn what to test and why it’s important to test and avoid technical debt.
So give it a try, it wont be difficult at all.