Question

I am working on tests but running in to a road block on pages that require a current_user. I am using minitest, capybara, factorygirl, and authlogic, in rails 3.2.9 with ruby 1.9.3p327. I installed minitest as a separate gem, and seem to have the test environment working correctly.

I have a factory that creates a valid user...I call that factory from in a test like this:

  describe "UsersAcceptanceTest" do
    it "must load and include content" do
      FactoryGirl.create(:user)
      visit users_path
      page.must_have_content("cPanel")
    end
  end

The FAIL is correct in informing me that the content "cPanel" was not found (cPanel is a link available to logged in users). The fail error goes on to alert me that it was not found in "log in, forgot password, contact" ... which of course means that the test routed correctly to users_path, but was redirected by authlogic because the user is not logged in. Users cant create themselves in my system and therefor are not auto-logged in on create.

How to I also get the factory to create a new user session with the newly created user?

Was it helpful?

Solution

You can do it this way:

  visit signin_path
  fill_in 'email',      with: user.email
  fill_in 'password',   with: user.password
  click_button "Log in"

Just edit it according to your login page structure.

I don't know about minitest, but in rspec I'd create the separate method with this codedef sign_in...end and put it to support\utilities.rb.

Then your code would be looking like that:

  describe "UsersAcceptanceTest" do
    let(:user) { Factory(:user) }
    subject { page }

    it "must load and include content" do
      sign_in user 
      visit users_path
      it { should have_link("cPanel",   href: cpanel_path) }
    end
  end

As you can see I've edited your code a little bit more.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top