rails functional test
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(开发者_JAVA百科)
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.
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.
精彩评论