Ruby rspec中的助手

Ruby rspec中的助手,ruby,rspec,automation,qa,helpers,Ruby,Rspec,Automation,Qa,Helpers,我有一个rspec项目,它被用作测试api的独立应用程序。我想找出将API调用定义为代码重用的助手的最佳方法 我设置它的方式是我有一个spec文件夹,其中包含一个支持文件夹和一个测试文件夹。在支持方面,我有一堆ruby文件,它们是用ruby模块/类设置的,用于定义规范中要使用的API调用 例如: module Helpers class Project def add(name: nil) api_call_code_goes_here end end e

我有一个rspec项目,它被用作测试api的独立应用程序。我想找出将API调用定义为代码重用的助手的最佳方法

我设置它的方式是我有一个spec文件夹,其中包含一个支持文件夹和一个测试文件夹。在支持方面,我有一堆ruby文件,它们是用ruby模块/类设置的,用于定义规范中要使用的API调用

例如:

module Helpers
  class Project

    def add(name: nil)
      api_call_code_goes_here
    end

  end
end
RSpec.shared_context "project" do

  def add_project(name: nil)
    api_call_code_goes_here
  end

end
这对我很有效,但我一直在阅读rspec中的共享上下文,不知道这是最佳实践还是对我更有效

例如:

module Helpers
  class Project

    def add(name: nil)
      api_call_code_goes_here
    end

  end
end
RSpec.shared_context "project" do

  def add_project(name: nil)
    api_call_code_goes_here
  end

end

有没有人有类似的rspec项目,有类似于我的助手?如果是这样,我想知道您是如何设置帮助程序的。

spec/support/helper文件中的Yes是在rspec中添加自己代码的最佳方法

我在support helper文件中添加了登录代码,如

module Login
  module Admin
   # this method creates a admin login for request spec.
   def login_admin
    @admin = FactoryGirl.create(:admin, password: '123456789')
    @current_admin = @admin
    visit new_admin_session_path
    fill_in 'admin_email', with: @admin.email
    fill_in 'admin_password', with: '123456789'
    click_button 'Sign In'
  end
end
在spec/spec\u helper.rb中需要此文件

Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
而且

config.include Login::Admin, type: :request

所以我可以使用超级管理员登录方法,任何请求规范…

您所做的一切都与我所做的非常相似,尽管有一个问题。config.include中的“type::request”是什么?该类型意味着帮助程序只包含for request规范。如果您为控制器编写了什么,则可以编写控制器。如果您跳过该类型属性,则表示您可以在任何规范中使用该帮助器。