5

Rails 菜鸟在这里。我无法理解如何/如何测试有关使用 Omniauth-Facebook 进行身份验证的内容。与相关的 railscast 几乎有类似的设置。我的测试环境设置类似于之前的问题和 Gem wiki 中描述的内容。

几个问题。当您创建用户工厂时,如何获得结果对象来模拟身份验证?

运行以下代码时还会发生什么?

before do 
  request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]
  visit '/auth/facebook'
end

对象是否保存在数据库中?

我在下面添加了一些示例规格和工厂规格。安装文件再次spec_helper将测试模式配置设置为 true。

add_mock设置:

OmniAuth.config.add_mock(:facebook,
                         { :provider => 'facebook',
                           :uid => '1234567',
                           :info => { :name => 'Jonathan', :image => 'http://graph.facebook.com/1234567/picture?type=square'},
                           :credentials => {
                             :expires_at => 1351270850,
                             :token=> 'AAADzk0b791YBAHCNhBI3n6ScmWvuXTY4yIUqXr9WiZCg1R808RYzaHxsHnrbn62IwrIgZCfSBZAVIP6ptF41nm8YtRtZCeBbxbbz1mF8RQZDZD'
                            } })  

用户页面规格:

describe "user pages" do

  let(:user) { Factory.create(:user) }

  describe "sign_in" do
    before { visit '/' }

    it "should add a user to the User model" do 
      expect { click_link "sign_in" }.to change(User, :count).by(1) 
    end

    it "should route to the appropriate page if email is nil" do
      click_link "sign_in"
      page.should have_selector('h5', text: 'Edit Email')
    end

    it "should redirect to the show page upon" do
      user.email = 'example@stanford.edu'
      user.save!
      click_link 'sign_in'
      page.should have_selector('div', text: user.name)
    end
   end
end

工厂

FactoryGirl.define do
  factory :user do
    provider  "facebook"
    uid       '1234567'
    name      'Jonathan'
  end
end
4

0 回答 0