0
votes

I am using rspec for testing devise authentication. Following is my code

require 'spec_helper'

describe User do

describe "user registration" do
it "allows new users to register with an email address and password" do
get "/users/sign_up"

fill_in "Email",                 :with => "[email protected]"
fill_in "Password",              :with => "abc123"
fill_in "Password confirmation", :with => "abc123"

 click_button "Sign up"

   response.should have_content("Welcome! You have signed up successfully.")
  end
 end
end

I am getting the following error.

"NoMethodError:undefined method `get' for #"

4

4 Answers

2
votes

You are using controller methods and integration test methods (Capybara) in a Model spec. It will not work.

A model spec (UNIT test) will contain things like:

  1. Test your validators/relationships
  2. Test scopes
  3. Methods of your model

Check out this series of Blog articles on testing with RSpec, it should help: http://everydayrails.com/2012/03/12/testing-series-intro.html

0
votes

This seems to be an model spec (describe User) which does not allow to run requests, but you probably want to write a controller spec (describe UsersController) or even an integration test.

If you are using the default rspec layout, just move your code to the appropriate directory (spec/controllers or spec/integration). I would do an integration test:

# In spec/integration/user_registration_spec.rb
require 'spec_helper'

describe "User registration" do
  it "allows new users to register with an email address and password" do
    get "/users/sign_up"

    fill_in "Email",                 :with => "[email protected]"
    fill_in "Password",              :with => "abc123"
    fill_in "Password confirmation", :with => "abc123"

    click_button "Sign up"

    response.body.should have_content("Welcome! You have signed up successfully.")
  end
end
0
votes

Is this file in the spec/models directory? I'm guessing that's the case since you're describeing a User. The way you wrote your test is a mix between a controller-style test and an integration (acceptance) test. This is probably what you want:

require 'spec_helper'

describe User do
  describe "user registration" do
    it "allows new users to register with an email address and password" do
      visit "/users/sign_up"

      fill_in "Email",                 :with => "[email protected]"
      fill_in "Password",              :with => "abc123"
      fill_in "Password confirmation", :with => "abc123"

      click_button "Sign up"

      page.should have_content("Welcome! You have signed up successfully.")
    end
  end
end

Put this file in the spec/integration or spec/requests directory.

0
votes

I would probably try something like this

require 'spec_helper'

describe User do

describe "user registration" do
 it "allows new users to register with an email address and password" do
  visit new_user_registration_path
  current_path.should be(new_user_registration_path)

  fill_in "user[email]",                 :with => "[email protected]"
  fill_in "user[password]",              :with => "abc123"
  fill_in "user[password_confirmation]", :with => "abc123"
  click_button "Sign up"

  expect { click_button submit }.to change(User, :count).by(1)
  response.should be_redirect   
  response.should have_content("Welcome! You have signed up successfully.")
  end
 end
end

But I can highly recommend to using FactoryGirl for generating new values. Also check, which Devise modules do you use. For example if you are using a Confirmable modul, is obvious that this approach is wrong. Some useful article.