2
votes

I have a helper method that calls current_user.eligible_for_reward? and I want to stub that call in a Capybara acceptance spec. In my model I have a placeholder dummy method:

User.rb

def eligible_for_reward?
  "blah"
end

Here is what I've tried so far:

  1. I tried stubbing current_user, but that results in a "modify frozen object" error because controller is nil. I believe controller will only be set if I use RSpec's get instead of visit some_path. I need to stick with visit some_path, I believe, otherwise my page.should ... all fail because page is not set.

  2. I tried logging in as user (login_as(user) which successfully runs through the register/login) and then stubbing user.eligible_for_reward?, but that doesn't seem to work, as I'm still getting back the test "blah"

Is there a good way to stub out this method?

1

1 Answers

8
votes

Stubbing in acceptance specs is not a good idea. If there is no other way then you can just do:

User.any_instance.stub!(:eligible_for_reward?).and_return(true)

Just remember that stubbing/mocking things in acceptance specs means that you really don't test anything.