Rails功能测试
在Rails自动生成的功能测试(test/function/products_controller_test.rb
)中,我看到以下代码:
class ProductsControllerTest < ActionController::TestCase
setup do
@product = products(:one)
...
end
...some tests here...
end
我的问题是:
方法调用在哪里/如何
products()
已定义?products(:one)
的实际含义是什么?看看代码,它可能意味着“创建一个产品”,但是它是如何工作的?
注意,我是 Ruby/Rails 的新手,如果这些问题很琐碎,我深表歉意。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您查看 test/fixtures 文件夹,您将看到一个 products.yml 文件。这是在您创建模型时自动生成的。在您的测试文件中,
products(:one)
指的是该文件中标记为“one”的产品。夹具会自动加载到数据库中进行测试,并且创建products()
方法作为这些对象的便捷访问器。您可以根据需要直接在 yml 文件中添加自己的夹具对象。
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 theproducts()
method is created as a convenient accessor for those objects.You can add your own fixture objects as needed, directly in the yml file.