0

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.

Prabhakar
  • 6,458
  • 2
  • 40
  • 51
  • Examples I've tried so far. it "should have all the todos in the index page" do visit "/todos" expect(todo.title).to eq(title) expect(todo.desc).to eq(desc) end But am not sure how can check the todos of current_user... etc. – Prabhakar Dec 09 '14 at 11:00
  • post your attempt in body question . – Roman Kiselenko Dec 09 '14 at 11:06
  • Yes, I wrote code. But I wrote the code just for CRUD operations which does not have anything to with current user. Now I want to write tests for the current user as well. here is the link for my test file, I wrote. [Link] (http://pastebin.com/j4m5jTv3) – Prabhakar Dec 09 '14 at 11:11
  • Read [this](http://stackoverflow.com/questions/6403650/rspec-devise-test-helpers) – Roman Kiselenko Dec 09 '14 at 11:13
  • I am actually not using Devise or any other for authentication. I am just manually doing it with bcrypt. – Prabhakar Dec 09 '14 at 11:19

1 Answers1

1

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.

Community
  • 1
  • 1
rafb3
  • 1,694
  • 12
  • 12