In the Rails auto-generated functional test (test/functional/products_controller_test.rb
), I see the following code:
class ProductsControllerTest < ActionController::TestCase
setup do
@product = products(:one)
...
end
...some tests here...
end
The questions that I have are:
Where/how is the method call products()
defined?
What does products(:one)
actually mean? Looking at the code, it probably means "create one product", but how does that work?
N.B. I'm new to Ruby/Rails, apologies if these are a trivial questions.
2.7 The Rails Test Runner We can run all of our tests at once by using the bin/rails test command. Or we can run a single test file by passing the bin/rails test command the filename containing the test cases. This will run all test methods from the test case.
The Tests − They are test applications that produce consistent result and prove that a Rails application does what it is expected to do. Tests are developed concurrently with the actual application. The Assertion − This is a one line of code that evaluates an object (or expression) for expected results.
While unit tests make sure that individual parts of your application work, integration tests are used to test that different parts of your application work together.
To run a Minitest test, the only setup you really need is to require the autorun file at the beginning of a test file: require 'minitest/autorun' . This is good if you'd like to keep the code small. A better way to get started with Minitest is to have Bundler create a template project for you.
If you look in your test/fixtures folder, you'll see a products.yml file. This is generated automatically when you create a model. In your test files, products(:one)
is referring to the product in that file labelled "one". Fixtures are automatically loaded into the database for tests, and the products()
method is created as a convenient accessor for those objects.
You can add your own fixture objects as needed, directly in the yml file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With