1
votes

I am having some difficulty in testing my login procedure. Currently, I have the following request spec:

     describe "GET /admins" do
        before(:each) do
          @admin = Factory.create(:user, :winery => nil, :email => '[email protected]', :admin => true)
          @attr = { :email => @admin.email, :password => @admin.password }
        end


    it "should log admin users in" do
      visit admin_path
      post_via_redirect admin_path, :session => @attr
      save_and_open_page
      page.should have_content('Admin Dashboard')
    end
  end

This is failing, and the save_and_open_page, just shows my login page (I can log in normally to my development site, so the actual code works). I have also tried writing the spec using Capybara to fill in the form and click the log in button, with the same failure. I have been able to write a passing spec in Cucumber, but only with selenium.

using: Capybara (0.4.1.2) Rspec (2.5.0) rspec-rails (2.5.0) rails (3.0.5)

thanks

2

2 Answers

5
votes

You should be using pure Capybara helpers here, post_via_redirect is a Rails method. You will first want to fill out the form then press some sort of submit button, like this:

visit admin_path
fill_in "Login", :with => "username"
fill_in "Password", :with => "password"
click_button "Submit"

Your final assert, I would be more precise and say in which selector that content should be shown, like this:

within_scope("h1") do
  page.should have_content('Admin Dashboard')
end

Now you're testing properly!

0
votes

After trying various ways to test this in both Rspec and cucumber, I finally found my self-inflicted problem.
In my form (using haml and formtastic) I had the following:

 = semantic_form_for @session, :as => :session, :url => { :action => "create" } do |form|
    = form.inputs do
      = form.input :email, :label => "Email"
      = form.input :password, :label => "Password"
    %fieldset.buttons
      %ol
        %li
          %button(name = "commit" type = "commit" value = "Login")Log In

This worked in Chrome and Firefox(and if a scenario was tagged @selenium), but in my tests, while the fill_in and click_button steps passed, the Create action in my controller was not being called. Changing the code for my button to this:

   = form.input :password, :label => "Password"
    = form.buttons do
      = form.commit_button "Log In"

Allows my tests to now pass. Not sure on the reason why, but at least I can get on with my integration tests