Rails - unit testing

I want to learn how to write unit tests for a Rails application. Where do I even start? All Google tutorials are old since 2007, and there seem to be plenty of options, but not a preferred solution. Suggestions appreciated.

+4
source share
3 answers

Most people now use rspec for unit testing and cucumber for integration testing. You can see a recent fairy poll here , where 87% chose rspec in the poll.

Great book for rspec and cucumber. Rspec book written by the current Rspec maintainer. It passes both rspec and cucumbers.

Railscasts also has some relevant screencasts. Cucmber1 , cucumber2

Many people seem to like cucumber, but this does not seem useful if you do not have a client for whom you work. Most of my projects are side projects that I do myself, so currently I am looking at steak instead of cucumber. This will allow me to use rspec for unit tests and steaks for integration tests and reduces some of the complexity / boredom that is introduced when using cucumber.

People said that it doesn’t really matter which test structure you choose, all the more important that you BEGIN TESTING. I agree with this, but I hope that these resources will help you get started.

However, on the one hand, you probably want to avoid from the very beginning - these are lights. Use factories instead and check out this episode of railscast on it.


UPDATE: the steak is no longer needed, and the same functions are baked into rspec.

+7
source

I suggest starting with Rspec by David Chelimsky and friends. It discusses different types of testing, why you should test and go through various examples that show you good practices, and also give recommendations on what to avoid.

Rspec is very popular and preferred by many over the default rails test suite. Most of the materials I read that go through the default test suit end up with an introduction to Rspec and how it makes life easier.

+6
source

If you want to do it right, you should use other tags like rspec , cucumbers or shoulda . This way you will write Unit Tests in the context of BDD. These frameworks are so obvious lately that the documentation you find about them is fairly modern! Finally, I suggest you read this about the external approach combining Rspec / Cucumber, about how convinced I was to use this framework, it explains very well how you should approach the tests!

+3
source

Source: https://habr.com/ru/post/1340556/


All Articles