Reputation: 6764
I am newbie to testing. Can someone help me how to test the current_user in rspec.
Example code:
Easy one:
def index
@todos = current_user.todos
end
a bit more hard one
def index
@events = current_user.participating_events.includes(
[:events_users, :participants, :user])
end
How can I write rspec tests for similar kind of problems.
Upvotes: 0
Views: 968
Reputation: 1702
Because it's an integration test, you should strive for actual login so that the spec manages the session created by the controller. You can follow the ideas on this answer, but basically you need to authenticate with a given user. But that you are already doing, so maybe the problem you are having is not clear by the question.
One thing I noticed weird on your tests is that event
is checked for some tests, but I can't see where it is defined. It's like you expected it to be set by rspec in some way. If that's the case, then that might be the problem you are having. For this kind of spec, you should load objects from the db to test if they were created, so after filling the form and saving the event, before testing the result, load it from the DB with
event = Event.last
and then probe it for the values stored.
Let me know if this makes sense and if this solves your problem. Otherwise, maybe try to clarify what are you trying to accomplish or post a backtrace of any error you are having.
Upvotes: 1