Create users in Factory Girl with OmniAuth?

空扰寡人 提交于 2020-01-14 10:16:17

问题


I am currently creating an application that uses OmniAuth to create and authenticate users. I am encountering problems during testing due to Factory Girl being unable to generate users without OmniAuth.

I have several different ways to get factory girl to create users with omniauth but none have been successful.

I have added the following 2 lines to my spec_helper file

OmniAuth.config.test_mode = true \\ allows me to fake signins
OmniAuth.config.add_mock(:twitter, { :uid => '12345', :info => { :nickname => 'Joe Blow' }})

current factories.rb

FactoryGirl.define do
  factory :user do
    provider "twitter"
    sequence(:uid) { |n| "#{n}" }
    sequence(:name) { |n| "Person_#{n}" }
  end
end

The following test currently fails because no user is being generated

let(:user) { FactoryGirl.create(:user) }
before { sign_in user }

describe "registering" do

  it "should increment" do
    expect do
      click_button 'register'
    end.to change(user.rounds, :count).by(1)
end

How should I change my factories/tests in order to get Factory Girl to create test users with OmniAuth?

Edit: I used the RailsCast guide to setup Omniauth,

#create function inside user.rb
def self.create_with_omniauth(auth)
  create! do |user|
    user.provider = auth["provider"]
    user.uid = auth["uid"]
    user.name = auth["info"]["name"]
  end
end

hopefully also useful

#create inside the session_controller
def create
  auth = request.env["omniauth.auth"]
  user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) ||    User.create_with_omniauth(auth)
  session[:user_id] = user.id
  redirect_to root_url, :notice => "Signed in!"
end

回答1:


Did you remember to do the following somewhere in the test setup?

request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter]

If you did, is it possible the user's UID doesn't match the mock uid?

You can try changing the factory definition from sequence(:uid) { |n| "#{n}" } to uid '12345'.



来源:https://stackoverflow.com/questions/11388100/create-users-in-factory-girl-with-omniauth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!