【发布时间】:2018-09-04 02:32:37
【问题描述】:
我想测试 else 分支。我在下面写了“测试这个分支”。要打那个分支@user.persisted?应该是假的。 from_omniauth 在用户模型中定义。
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.from_omniauth(request.env["omniauth.auth"])
if @user.persisted?
flash[:notice] = I18n.t 'devise.omniauth_callbacks.success', kind: 'Facebook'
sign_in_and_redirect @user, event: :authentication
else
//TEST THIS BRANCH
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_session_url, alert: @user.errors.full_messages.join("\n")
end
end
end
User模型定义如下:
class User < ApplicationRecord
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email ? auth.info.email : Devise.friendly_token[0,20] + "@" + Devise.friendly_token[0,20] + ".com"
user.password = Devise.friendly_token[0,20]
end
end
end
我正在使用 rspec 和 capybara 进行功能测试。我如何模拟 self.from_omniauth(auth) 以便我可以测试 else 分支?我很欣赏有关如何正确执行此操作的任何见解。谢谢!
【问题讨论】:
-
看看this是否有帮助。
标签: ruby-on-rails capybara omniauth rspec-rails