1
votes

I've been stuck trying to get my requests spec to work. I've tried a number of different approaches I've found thru Google and SO. Below are the following helpers I've tried separately with my request spec:

#spec/support/devise_request_spec_support.rb
module DeviseRequestSpecSupport

  def login(user)
    post user_session_path, login: user.email, password: 'foobar'
  end
end

#spec/support/utilities.rb
include ApplicationHelper

def valid_login(user)
  fill_in "user[email]", with: user.email
  fill_in "user[password]", with: 'foobar'
  click_button "Sign in"
end

def login(user)
  visit root_path
  valid_login(user)
end

I've tried both above #login methods separately in my Request spec below:

require 'spec_helper'

describe "Company Admin Dashboard", js: true do
    let(:user) { Fabricate(:company_admin) }

    before do
        login(user)
        poll = Fabricate(:poll)
        poll.send_to_all_users
    end

  describe "interacting with the poll box" do
    it "displays sent poll in poll box" do
      visit root_path
    end
  end
end

The test will pass but the root_path doesn't display the landing page meant for a logged in user. It shows the public landing page. I've tried debugging with binding.pry and tried logging in manually in test environment but I keep getting an 'Invalid email or password' error. I've checked many times but I've been using the correct email and password of the Fabricated company_admin.

I've also tried the solution proposed here too: https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara

Any help would be greatly appreciated. Not sure where the problem lies. Is it the Fabrication gem, Devise, RSpec or Capybara?

2
Did you solve it? How? I SUDDENLY have exactly same error.ExiRe
@ExiRe haven't solved it yet. Are you using Fabrication too?Gjaldon

2 Answers

0
votes

You could try using 'let!' instead of 'let' to create the User. Suspect that your user hasn't been created when you try to login because you're using the lazy version of 'let'.

0
votes

I had once a same error. My error appeared suddenly and it was a big surprise. You should note that i have same gems to test: Devise, RSpec, Capybara and FactoryGirl (instead of Fabrication).

But there is the difference between our problems: my error appear suddenly, so i just created a separate branch where i reverted master to previous commit and i found that somehow i changed the spec_helper.rb (i implemented disabling garabage collector). I reverted commit and then my tests passed.

I suppose you can try next things:

1) Make sure that ALL gems that you are using to test are up to date by running bundle outdated

2) Try to change visit root_path to visit '/'

3) Launch rails c test, create a test user and make sure, that saved password is match to the test's one. Check out here how to do that

4) Try to use FactoryGirl instead of Fabrication if nothing helps