Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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() 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.

like image 425
ryanprayogo Avatar asked Dec 04 '10 04:12

ryanprayogo


People also ask

How do I run a Rails test?

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.

What is unit test rails?

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.

What is Rails integration test?

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.

How do you run a Minitest in rails?

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.


1 Answers

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.

like image 107
Jaime Bellmyer Avatar answered Oct 10 '22 07:10

Jaime Bellmyer