Setup and teardown methods in ruby
In another words:. There are many, many nuances to fixtures e. It is used for parametrization. You can see, we have used a. So, data. So this can be solved by using two methods:. Fixtures are functions, which will run before each test function to which it is applied. You can read more with examples here. When a setUp method is defined, the test runner will run that method prior to each test. Likewise, if a tearDown method is defined, the test runner will invoke that method after each test.
For example you have a test that requires items to exist, or certain state — so you put these actions creating object instances, initializing db, preparing rules and so on into the setUp.
So the idea is that test itself should contain only actions that to be performed on the test object to get the result, while setUp and tearDown are the methods to help you to leave your test code clean and flexible.
You can create a setUp and tearDown for a bunch of tests and define them in a parent class — so it would be easy for you to support such tests and update common preparations and clean ups. If you are looking for an easy example please use the following link with example. You might use this for creating test data e. In short, it takes much more time to setup and make it work. But once it's done, the testing code is much cleaner than RSpec, which is the best part I feel from Minitest.
If you're using spec framework just set it to true , which I would not recommend because comparing with Minitest::Spec , RSpec is definitely a better choice. Also, I prefer using factory to fixtures, so I set it to false as well. The first test is fairly simple. Supposedly we have a Post model and we want to test that it can be saved successfully. Class name should be suffixed by Test , and inherited from any Minitest class.
Also, methods should always start with test. For example:. Note that from Rails 4.
0コメント