简体   繁体   中英

Failing Test in Rails with Minitest: Why is my seed not working? Why does it work if I use fixtures instead?

I've started learning how to test with Ministest. I have a side project that I'm using to run the tests.

I'm running a simple test where a user logs in, visits the projects index and attempts to create a new project.

I'm seeding via console with:

rake db:seed RAILS_ENV=test

There is a modal window at the index that asks the user to add "technologies" to his "skillset". I'm making the test close the modal so it can click on the Create Project button like so:

  test "a logged in user can create a project" do
    login_as users(:admin)
    visit projects_url
    click_on "Later" // close modal window
    click_on "Create Project"
    assert_equal new_project_path, page.current_path
  end

However, I'm getting errors related to the modal window itself, because Technology.first, etc. is nil. As if the seed was not running and populating the DB.

Modal where Technology instances are used:

<div class="modal-body">
    <p class="mr-3 mb-0" style="font-size: 1.4rem; text-align: center;">
       <%= link_to (sanitize pick_tech_icon(Technology.first.name), tags: %w(i), attributes: %w(class style)) %>
       <%= link_to (sanitize pick_tech_icon(Technology.second.name), tags: %w(i), attributes: %w(class style)) %>
       <%= link_to (sanitize pick_tech_icon(Technology.third.name), tags: %w(i), attributes: %w(class style)) %>
       <%= link_to (sanitize pick_tech_icon(Technology.fourth.name), tags: %w(i), attributes: %w(class style)) %>
       <%= link_to (sanitize pick_tech_icon(Technology.fifth.name), tags: %w(i), attributes: %w(class style)) %>
    </p>
</div>

Console error:

Error:
ProjectsTest#test_a_logged_in_user_can_create_a_project:
ActionView::Template::Error: undefined method `name' for nil:NilClass
    app/views/shared/_add_skill_modal.html.erb:13
    app/views/projects/index.html.erb:1

Why is my seed not working? Why does the test run OK if I use a Technology fixture to create Technology instances instead? I thought fixtures were not supposed to replace the DB data but rather serve as data to test against.

In Rails 4.x we have transactional fixtures that wrap each test in a database transaction. This transaction rollbacks all the changes at the end of the test. It means the state of the database, before the test is same as after the test is done.

By default this functionality is enabled. We can choose to disable it in a test case class by setting the class attribute use_transactional_fixtures to false

However,

To overcome this confusion, Rails 5 has renamed transactional fixtures to transactional tests making it clear that it has nothing to do with the fixtures used in tests.

Reference: https://blog.bigbinary.com/2016/05/26/rails-5-renamed-transactional-fixtures-to-transactional-tests.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM