Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test cases failing #18

Open
mohakjuneja1 opened this issue Apr 27, 2015 · 5 comments
Open

Test cases failing #18

mohakjuneja1 opened this issue Apr 27, 2015 · 5 comments

Comments

@mohakjuneja1
Copy link

def current_user
@current_user ||= User.find_by(auth_token: request.headers['Authorization'])
end

when i check, request.headers['Authorization'] equals null and all my test cases fail. What am I doing wrong?

@kurenn
Copy link
Owner

kurenn commented Apr 27, 2015

Do you have everything setup on the spec_helper.rb file?:

  config.include Request::JsonHelpers, :type => :controller
  config.include Request::HeadersHelpers, :type => :controller
  config.include Devise::TestHelpers, :type => :controller

@mohakjuneja1
Copy link
Author

Yes I included all of them. Can you take a look at my repository and tell what i'm doing wrong? Here's the link- https://github.com/Mohakjuneja/FatBoy

I'd be really grateful. Thanks!

@kurenn
Copy link
Owner

kurenn commented Apr 28, 2015

Check out the pull request I made Mohakjuneja/FatBoy#1

@mohakjuneja1
Copy link
Author

Hey! Thank you for your support. All the test cases are passing except two.
I have pushed your recommended changes to my repository (link up there) except the change in user.rb because I end up failing more test cases.
I also moved the authenticate_with_token! method back to users controller

Can you take a look at these?

in sessions_controller_specs, on line47, @user = FactoryGirl.create :user has attribute auth_token = nil. How will find the user with a nil auth_token?

  1. Api::V1::SessionsController DELETE #destroy
    Failure/Error: delete :destroy, id: @user.auth_token
    ActionController::UrlGenerationError:
    No route matches {:action=>"destroy", :controller=>"api/v1/sessions", :id=>nil}

    ./spec/controllers/api/v1/sessions_controller_spec.rb:50:in `block (3 levels) in <top (required)>'

Devise.stub(:friendly_tokenn)and_return("auniquetoken123") returns nil
@user.generate_authentication_token! equals nil

  1. User#generate_authentication_token! generates a unique token
    Failure/Error: expect { @user.generate_authentication_token! }.to change{@user.auth_token}.from(nil).to("auniquetoken123")
    result should have been changed to "auniquetoken123", but is now nil

    ./spec/models/user_spec.rb:21:in `block (3 levels) in <top (required)>'

Thank you again!

@kurenn
Copy link
Owner

kurenn commented May 1, 2015

The thing in here is that you need to have a auth_token for each user and in the first case, it seems that it does not have one. A good idea would be to put it on the users.rb factory file:

FactoryGirl.define do
  factory :user do
    email { FFaker::Internet.email}
    password "12345678"
    password_confirmation "12345678"
    auth_token { Devise.friendly_token }
  end
end

On the second one I think you have to specify the self to refer to the instance when trying to assign the auth_token, something like:

  def generate_authentication_token!
    loop do
      self.auth_token = Devise.friendly_token
      break auth_token unless User.where(auth_token: auth_token).first
    end
  end

Ruby may not know wheter you are refering to a variable or the attribute, and in this case is to a variable I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants