简体   繁体   English

模拟用户登录Rspec-使用Faraday和Github-Api Gems

[英]Mock User Sign In Rspec - Using Faraday and Github-Api Gems

I am using Faraday and Github-Api gems to log a user in with Github. 我正在使用Faraday和Github-Api宝石使用Github登录用户。 In order to test any type of functionality, I have to mock this behavior. 为了测试任何类型的功能,我必须模拟这种行为。 I am thinking that in my spec, I should say something like "if the new action is called (which is what pings the github-api), then redirect to the create action and pass in the parameters that it needs to create a user." 我认为在我的规范中,我应该说类似“如果调用了新动作(对github-api执行ping操作),然后重定向到create动作并传递创建用户所需的参数。 “ I am not entirely sure how to do this. 我不太确定该怎么做。 I realize that there are solutions for Oauth, modules with helper methods to mock this behavior, but I am adding tests to an app that someone else built so it has to stay as is. 我意识到有针对Oauth的解决方案,即带有辅助方法的模块来模拟这种行为,但是我正在为其他人构建的应用添加测试,因此必须保持原样。 Any help or pointers would be greatly appreciated. 任何帮助或指针将不胜感激。 I'm fairly new to testing as well so feel free to comment on the test itself. 我对测试也相当陌生,因此请随时对测试本身发表评论。

Spec 规格

require "rails_helper"

RSpec.feature "User submits a project" do
  scenario "they see the index page for their projects" do

    project = create(:project)

    visit '/projects/new'

    fill_in "project_title", with: project_title
    fill_in "project_project_type", with: project_project_type
    fill_in "project_description", with: project_description
    fill_in "project_starts_at", with: project_starts_at
    click_on "Create Project"

    redirect_to '/projects'

    expect(page).to have_project_title
  end
end

Sessions Controller 会话控制器

def new
  redirect_to "githubapiurlgoeshere"
end

def create
  @user = User.find_or_create_from_auth_hash(auth_hash)
  if @user.save
    session[:token] = @user.token
    session[:user_id] = @user.id
    redirect_to root_path
  else
    flash[:notice] = "message here"
    redirect_to root_path
  end

end

webmock ( https://github.com/bblimke/webmock ) ? webmock( https://github.com/bblimke/webmock )吗? It will allow you to mock the connection to Github with pre-defined response so it will be as close to reality as it can be + you can define variables inside the response 它将允许您使用预定义的响应来模拟与Github的连接,从而使其尽可能接近实际+您可以在响应中定义变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM