5
votes

I've seen tons of these questions, but none of their solutions are working for me. I have a single test like so:

describe RolesController do
  describe "#delet" do 

    context "When the user is logged in" do 
      let(:user) {FactoryGirl.create(:user)}
      let(:admin) {FactoryGirl.create(:admin)}
      let(:adminRole) {FactoryGirl.create(:adminRole)}

      it "Should allow admins to delete roles" do 
        sign_in admin
        put :destroy, :id => adminRole.id
      end
    end
  end
end

Simple, simple, simple. Yet I get the typical error:

  1) RolesController#delet When the user is logged in Should allow admins to delete roles
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /home/adam/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/notifications/instrumenter.rb:23

and I'm all like ... what? Again I have read dozens of questions on this and it seems to be something with factory girl but I cannot see what the issue here would be. I have tons of other tests that instantiate factory girl based object like this with no issue.

2
What does your stacktrace look like? In any event, you'll need to share your factories in order to get more specific help than is in the related questions. - Peter Alfvin
be careful with let - it is lazy. When your test runs sign_in admin, it will instantiate the admin before running the test, but not the user or adminRole. - zetetic
The error indicates that you've probably triggered an infinite loop. It may have to do with a cyclical dependency between your factories. Could you post the factory definitions? - Karl Higley
Can you post the code called by sign_in admin along with the factory code for admin and adminRole please? - Jon
This is not what you asked for but you should rename adminRole to admin_role. Rubyists uses snake_case for variable names. - Pascal

2 Answers

1
votes

Simply do not use the variable you are defining with let inside the following code block as it triggers an endless recursion.

-1
votes

I think some naming conflicts causing this stack level too deep error.

let(:adminRole) { FactoryGirl.create(:adminRole) }

How about you rename the adminRole factory to :admin_role:

factory :admin_role do
  ...
end

And change the let to follow variable convention:

let(:admin_role) { FactoryGirl.create(:admin_role) }

Hope this may help you.