Testing with RSpec and Factory Girl

EDIT Read my comment on this question.

I am very new to rails, so please carry me. I tried to set up a test for Devise using factory girls and rspec. It took me most of 2 hours, and cleaning half of the Internet is useless. Despite the fact that my problem has a lot of workloads, I just can not understand it.

This is what my / spec files look like.

GET Home Gives the correct status code
     Failure/Error: sign_in user
     NoMethodError:
       undefined method `sign_in' for #<RSpec::Core::ExampleGroup::Nested_2:0x00000106f32558>
     # ./spec/models/user_spec.rb:6:in `block (2 levels) in <top (required)>

This is the error message that I am getting while trying to run the following test:

user_spec.rb:

require 'spec_helper'

describe "GET Home" do
  before do

##I have tried all sorts of things here. I have also tried to define a module in devise.rb (see note below*), and then call that module here instead of the 2 lines below. But I get the same error, no local variable or undefined method for ...

    user = FactoryGirl.create(:user)
    sign_in user
  end

  describe "GET /Home"
  it "Gives the correct status code" do
    get root_path
    response.status.should be(200)
  end


end

in spec/factories/users.rb:

FactoryGirl.define do 
    factory :user do 
        name "Christoffer"
        email "test@test2.com" 
        password "testtest" 
        password_confirmation "testtest" 
    end 
end

And auxiliary lines are included in spec_helpers.rb

config.include FactoryGirl::Syntax::Methods
config.include Devise::TestHelpers, :type => :controller

Now, having done this, I will get the error above. Can someone explain what I'm doing wrong here? It could be something really obvious, since I did not rehearse very well in Rails.


* (, devise.rb before do):

module ValidUserRequestHelper
    # Define a method which signs in as a valid user.
    def sign_in_as_a_valid_user_nommels
        # ASk factory girl to generate a valid user for us.
        @user ||= FactoryGirl.create :user

        # We action the login request using the parameters before we begin.
        # The login requests will match these to the user we just created in the factory, and authenticate us.
        post_via_redirect user_session_path, 'user[email]' => @user.email, 'user[password]' => @user.password
    end
end
+4
1

"spec/requests" - . (.. , , , ). / / , ( , , _, , )

0

Source: https://habr.com/ru/post/1534247/


All Articles