Facebook omniauth raw info auth额外的模拟配置设置

时间:2016-03-14 18:42:45

标签: facebook testing capybara omniauth

我尝试使用facebook测试一个简单的omniauth登录信息。但是,收到错误消息“auth'未定义variablemethodomniauth测试文档没有包含facebook示例

运行黄瓜特征测试

undefined local variable or method `auth' for #<Cucumber::Rails::World:0x007fa17eeafc78> (NameError)
      ./features/step_definitions/microsites/quiz/microsites_quiz_entrant_sign_in_facebook_steps.rb:26:in `/^I log in with my facebook email and password quiz$/'

steps.rb文件

Then(/^I log in with my facebook email and password quiz$/) do
  OmniAuth.config.add_mock(:facebook, {
    :provider => "facebook",
    :uid => '12345',
    :name => "John Smith",
    info: {
      email: "test@test.com",
      first_name: "Test",
      last_name: "Tester",
      gender: "male",
      dob: auth.extra.raw_info.birthday
    }
  })
end

配置/环境/ test.rb

OmniAuth.config.test_mode = true
  OmniAuth.config.mock_auth[:twitter] = OmniAuth::AuthHash.new({
    :provider => 'twitter',
    :uid => '123545'
  })

模型文件

  def self.from_facebook auth, campaign, password
    campaign.entrants.where(fb_uid: auth.uid).first_or_create do |entrant|
      entrant.fb_uid                = auth.uid
      entrant.email                 = auth.info.email
      entrant.first_name            = auth.info.first_name
      entrant.last_name             = auth.info.last_name
      entrant.password              = password
      entrant.password_confirmation = password
      entrant.campaign_id           = campaign.id
      # DOB is not returned from FB consistently, or not at all
      entrant.dob                   = auth.extra.raw_info.birthday
      # If DOB is returned, this create will pass, and this line should fire:
      entrant.skip_confirmation!
    end
  end

1 个答案:

答案 0 :(得分:1)

您正在嘲笑auth响应,因此您无权访问auth对象以获取dob - 而是您需要类似

的内容
OmniAuth.config.add_mock(:facebook, {
  :provider => "facebook",
  :uid => '12345',
  :name => "John Smith",
  info: {
    email: "test@test.com",
    first_name: "Test",
    last_name: "Tester",
    gender: "male"
  },
  'extra' => {
    'raw_info' => { 'dob' => '1980-01-01 or whatever format is returned   by Facebook'}
  }
})