← Back to Upcase

SessionsHelper in my spec files


(charlieanna) #1

How do I include SessionsHelper methods into my specs files?
like current_user and sign_in
Currently I am getting undefined local variable or method `current_user’ for #RSpec::Core::ExampleGroup::Nested_1:0x007ffd4391ac60

I am trying to use

  expect(page).to have_content "Welcome #{current_user.name}"

in a spec file.


(charlieanna) #2
require "spec_helper"
include SessionsHelper
describe "Groups" do

  it "signed in user should be able to create a group", :js => true do
    @user = User.create(name:"ankur",email:"ankothari@gmail.com",password:"akk322",password_confirmation:"akk322")
    subject{@user}
	  visit '/'
	  click_link "Log In"

	  fill_in "session_email",with: @user.email
	  fill_in "session_password",with:@user.password
	  
	  within("#login-content") do
	  
     click_button "Login"
     
	   
    end
     # fill_in "creategroupinput", with: "Electronics"
     
      expect do
        xhr :post, user_groups_path(@user), groups: { name:"Electronics" }
      end.to change(@user.groups, :count).by(1)
     # # expect{click_button "Create"}.to change{user.groups.count}.by(1)
     # expect{click_button "Create Group"}.to change{user.groups.count}.by(1)
     
  end	

I am getting this error

Failure/Error: xhr :post, user_groups_path(@user), groups: { name:"Electronics" }
     NoMethodError:
       undefined method `groups' for nil:NilClass

Inside my groupscontroller def create I have a current_user.groups which gives me nil.

How do I test ajax without changing my page? How can I test this?


(Chad Pytel) #3

@ankur_kothari, the error you’re getting means the current_user is nil, which means the login probably didn’t work. Also, it looks like you’re mixing capybara and xhr, which might use a different session. Thats where you’ll want to investigate.

As an aside, when you paste code, can you try to make it better formatted? Properly spaced and indented? That’ll help us be able to read it and help you.

thanks,
-Chad