0

我正在尝试测试 AJAX 请求,并被告知将 poltergeist 与水豚一起使用以使其正常工作。即使它被嘲笑,我的测试在 Twitter 授权期间也失败了。这是我的规格:

def valid_sign_in_via_twitter
    visit root_path
    mock_omniauth_sign_in
    click_link "Sign in with"
end

describe '- input form via AJAX -', :js => true do
    it 'input form is valid if all fields are filled in.', :driver => :poltergeist do
        valid_sign_in_via_twitter
        valid_tweet_form_input
        expect(page).to have_content("Your tweet has been scheduled")
        expect(Tweet.all.count).to be(1)
    end

    it 'will be added to delayed_job process queue when scheduled.', :driver => :poltergeist do
        valid_sign_in_via_twitter
        valid_tweet_form_input
        expect(page).to have_content("Your tweet has been scheduled")
        expect(Delayed::Job.count).to eq(1)
        expect(Delayed::Job.last.handler).to have_content("This is a test scheduled tweet.")
    end
end

这是模拟oauth登录助手:

module OmniauthHelper
  def mock_omniauth_sign_in
    OmniAuth.config.mock_auth[:twitter] = OmniAuth::AuthHash.new({
    :provider => 'twitter',
    :uid => '1234567',
    :user_info => {
        :nickname => 'estlintester'
    },
    :credentials => {
        :token => '2342kmlskdlakmsdlkasmthisisfake',
        :secret => 'aslsakmdlkm32m3902jthisisfake'
    }
  })
  end
end

我可以告诉测试在方法中的“click_link”处失败valid_sign_in_via_twitter。这是错误,我相信它遇到了内部服务器错误:

An error occurred in an after hook
  NoMethodError: undefined method `[]' for nil:NilClass
  occurred at /Users/alex/Documents/Side
Projects/estlin/app/models/user.rb:26:in `block in create_from_omniauth'

这是 user.rb 中与错误有关的代码,第 26 行是user.nickname = auth["info"]["nickname"]

def self.from_omniauth(auth)
  user = User.where(auth.slice("uid")).first || create_from_omniauth(auth)
  if user.oauth_token != auth["credentials"]["token"] || user.oauth_token != auth["credentials"]["secret"]
    user.oauth_token = auth["credentials"]["token"]
    user.oauth_secret = auth["credentials"]["secret"]
    user.save!
  end
  user
end

def self.create_from_omniauth(auth)
  create! do |user|
    user.uid = auth["uid"]
    user.nickname = auth["info"]["nickname"]
    user.oauth_token = auth["credentials"]["token"]
    user.oauth_secret = auth["credentials"]["secret"]
  end
end

这是我的控制器中的创建操作,在点击登录链接时通过 Twitter 后被点击:

def create
  user = User.from_omniauth(request.env['omniauth.auth'])
  session[:user_id] = user.id
  redirect_to root_path, notice: "You have signed in as '#{user.nickname}'"
end 

这是我的 application_controllersession[:user_id]使用的地方:

def current_user
    @current_user ||= User.find_by_id(session[:user_id]) if session[:user_id]
end

在我实现 AJAX 之前,过去的测试可以顺利通过,我完全迷失了。由于身份验证失败,我什至无法测试 AJAX 请求。该应用程序也运行良好,没有问题,只是测试失败了。不知道在这里做什么。我需要对 VCR/FakeWeb 做些什么吗?我以前没有用过这些,所以我不确定。

4

1 回答 1

0

顺便提一句!在您的 OmniAuth.config.mock_auth[:twitter] 您有一个:user_info密钥,但在用户模型 (create_from_omniauth) 中,您试图获取 auth[" info "]["nickname"]

改变这个:

def self.create_from_omniauth(auth)
  create! do |user|
    user.uid = auth["uid"]
    user.nickname = auth["info"]["nickname"]
    user.oauth_token = auth["credentials"]["token"]
    user.oauth_secret = auth["credentials"]["secret"]
  end
end

对此:

def self.create_from_omniauth(auth)
  create! do |user|
    user.uid = auth["uid"]
    user.nickname = auth["user_info"]["nickname"]
    user.oauth_token = auth["credentials"]["token"]
    user.oauth_secret = auth["credentials"]["secret"]
  end
end

这可能会为您解决问题

于 2013-08-15T07:04:27.833 回答