【问题标题】:How remove github repository dependency from a test如何从测试中删除 github 存储库依赖项
【发布时间】:2014-03-19 19:05:26
【问题描述】:

我有以下测试:

describe "POST 'create'" do
  let(:url){ Rails.root.join('spec','files','example.git').to_s }

  it "redirect" do
    post 'create', repository: {url: url }

    expect(response).to be_redirect
  end
end

我的控制器代码:

def create
  @repository = Repository.new repository_params

  process_and_assign_respository_data

  respond_to do |format|
    if @repository.save
      format.html { redirect_to @repository }
    else
      format.html { render action: 'new' }
    end
  end
end

private

def process_and_assign_respository_data
  path = get_path
  name = Time.now.to_i.to_s

  Git.clone @repository.url, name, path: path
end

创建操作会克隆一个存储库。
测试在本地通过,但在 Travis 中中断并显示:

Failure/Error: post 'create', repository: { url: Rails.root.join('spec','files','example.git').to_s }

Git::GitExecuteError:

git clone "--" " &1:fatal: repository 'example.git' does not exist

我通过将 url 更改为 github 存储库来修复它。
有没有办法消除对 github 存储库的依赖而不破坏 Travis?

【问题讨论】:

  • 请粘贴控制器方法。

标签: ruby-on-rails ruby rspec


【解决方案1】:

repo 是否是有效的 Github url 并不重要。你只需要假数据来测试,无论是夹具还是工厂,而我更喜欢 FactoryGirl。

# Define it in factory file
factory :repo do
  url: 'http://example.com/abc.git'
end

# Then test
describe "POST 'create'" do
  let(:repo_attr) { FactoryGirl.attributes_for :repo }

  it "redirect" do
    post 'create', repository: repo_attr
    expect(response).to be_redirect
  end
end

【讨论】:

  • 没用 问题不是url,而是repository路径,在travis上传递本地路径时没有找到repository,只有我用github、bitbucket或者类似的时候才通过
  • 我明白了,你在真正的 repo 上做了一些工作。因此,您需要模拟process_and_assign_respository_data 的响应以返回虚假结果以获取测试通过。此方法与本次测试无关,可以模拟。
  • 嘲笑process_and_assign_respository_data工作
猜你喜欢
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-19
  • 2018-11-14
  • 2017-06-03
  • 1970-01-01
相关资源
最近更新 更多