1
votes

I have a Roles model that needs 'student', 'instructor', 'admin' rows before any tests are run. I can create (and pass tests) for a user with associated student role, but this only creates the one role. But, methods and scopes in my app fail because they may be expecting to find an 'admin' role...that isn't there.

How do I create multiple roles in FactoryGirl before my tests run?

I tried this...

In minitest_helper.rb

 class MiniTest::Spec  
   include FactoryGirl::Syntax::Methods
   before :each do
     DatabaseCleaner.clean
     Capybara.reset_sessions!
     Capybara.use_default_driver

     FactoryGirl.create(:role, name: "student")
     FactoryGirl.create(:role, name: "admin")
     FactoryGirl.create(:role, name: "instructor")
   end
  end

The roles are created as expected...

In factories.rb

    factory :account do
      user # associated user factory

      trait :student do
       role_id { Role.find_by_name("student") }
      end
    end

I would expect this to assign the correct student role id to the accounts role_id field when the following is called in the test...

In user_test.rb

   it "says Welcome Back" do
     a = FactoryGirl.create :account, :student
     assert page.has_content?('Welcome Back'), "does not contain Welcome Back" 
   end

The error I get is:

  undefined method `to_i' for #<Role:0x007fd5563cbdc8>
1

1 Answers

1
votes

changed

 role_id { Role.find_by_name("student") } 

to

role_id { Role.find_by_name("student").id }

Thats all it took...