Omniauth Rspec测试问题

Whe*_*uys 11 rspec ruby-on-rails omniauth

我按照omniauth的轨道演员创建了twitter的身份验证(http://railscasts.com/episodes/235-omniauth-part-1?view=comments).它在开发中工作正常但我无法让rspec检测到我已经创建了身份验证.这是我的身份验证控制器中创建功能的代码段:

def create
  begin

    auth_hash = request.env["omniauth.auth"]
    @auth = current_user.authentications.build( :provider     => auth_hash['provider'], 
                                              :uid          => auth_hash['uid'],
                                              :name         => auth_hash['user_info']['name'],
                                              :nickname     => auth_hash['user_info']['nickname'],
                                              :image        => auth_hash['user_info']['image'] 
                                              )

    if @auth.provider.downcase == "twitter"
      @auth.auth_token      = auth_hash['credentials']['token']
      @auth.secret_token    = auth_hash['credentials']['secret']
      @auth.site            = auth_hash['user_info']['urls']['Twitter']
    elsif @auth.provider == "Facebook"

    end

  rescue
    redirect_to current_user, :flash => { :error => "Missing oauth data!! Check with administrator!"}
  else
    if @auth.save
      msg = "Authentication success"
    else
      msg = "Already have authentication"
    end
    redirect_to current_user, :notice => msg
  end
end
Run Code Online (Sandbox Code Playgroud)

包含在我的路线中:

match '/auth/:provider/callback' => 'authentications#create'
Run Code Online (Sandbox Code Playgroud)

我的rspec_helper中有以下设置:

OmniAuth.config.test_mode = true
OmniAuth.config.add_mock(:twitter, {  :provider    => "twitter", 
                                  :uid         => "1234", 
                                  :user_info   => {   :name       => "Bob hope",
                                                      :nickname   => "bobby",
                                                      :urls       => {:Twitter => "www.twitter.com/bobster"}},
                                  :credentials => {   :auth_token => "lk2j3lkjasldkjflk3ljsdf"} })
Run Code Online (Sandbox Code Playgroud)

这是我的rspec代码不起作用:

  describe "Post 'create'" do
    before(:each) do
      @user = Factory(:user)
      sign_in @user
    end

    describe "success" do
      before(:each) do
        request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter]
      end


      it "should create authentication" do
        lambda do
          post :create, :provider => "twitter"
          response.should redirect_to(@user)
        end.should change(Authentication, :count).by(1)
      end
    end
  end
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

1)AuthenticationsController发布'创建'成功应该创建认证失败/错误:lambda do count应该被改变1,但被0#./spec/controllers/authentications_controller_spec.rb:57改变了

我检查了一切,无法弄清楚我做错了什么.有人可以帮忙吗?

Whe*_*uys 3

我终于明白出了什么问题。在我的模拟中:auth_token 应该是:token。这导致了验证失败。