开发者

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:

  1. Where/how is the method call products(开发者_JAVA百科) defined?

  2. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜