如何为与Git一起使用的ruby脚本编写rspec测试

时间:2015-09-21 05:15:47

标签: ruby git unit-testing rspec

我有一个Ruby脚本,用于检查git repo和url端点上的文件之间的变化。如果存在更改,它会提交它们并将它们推送到repo。我想为这个脚本编写rspec测试,但是我无法理解如何这样做,因为我不希望测试实际上将任何东西推送到实际的Git仓库。我想我需要建立某种模拟存储库,但我不完全确定。关于如何最好地为这样的脚本编写单元测试的任何建议将不胜感激。

1 个答案:

答案 0 :(得分:2)

我认为您可以利用这样一个事实:推送到本地“裸”存储库是完全合法的,即您需要执行以下步骤:

  1. 创建一个“裸”存储库repo1(即存储库,没有工作副本,它完全由.git文件夹的内容组成)
  2. repo2克隆常规存储库repo1
  3. repo2中那样(添加,提交,推送)进行任何更改。检查推送是否成功。
  4. 检查数据是否也在repo1中可用。
  5. 以下是我/tmp目录中的示例控制台会话:

    ruby-2.2.0 in /tmp ♥ mkdir repo1
    ruby-2.2.0 in /tmp ♥ cd repo1 
    ruby-2.2.0 in /tmp/repo1 ♥ git init --bare
    Initialized empty Git repository in /private/tmp/repo1/
    ruby-2.2.0 in /tmp/repo1 ♥ cd ..
    ruby-2.2.0 in /tmp ♥ git clone repo1 repo2
    Cloning into 'repo2'...
    warning: You appear to have cloned an empty repository.
    done.
    ruby-2.2.0 in /tmp ♥ cd repo2 
    ruby-2.2.0 in /tmp/repo2 on master ♥ git remote -v
    origin  /tmp/repo1 (fetch)
    origin  /tmp/repo1 (push)
    ruby-2.2.0 in /tmp/repo2 on master ♥ cd ..
    ruby-2.2.0 in /tmp ♥ cd -
    /tmp/repo2
    ruby-2.2.0 in /tmp/repo2 on master ♥ echo 'ohai!' >> readme.txt
    ruby-2.2.0 in /tmp/repo2 on master ♥ git add readme.txt 
    ruby-2.2.0 in /tmp/repo2 on master ♥ git commit
    [master (root-commit) 1f1fb96] First commit
     1 file changed, 1 insertion(+)
     create mode 100644 readme.txt
    ruby-2.2.0 in /tmp/repo2 on master ♥ git push
    Counting objects: 3, done.
    Writing objects: 100% (3/3), 227 bytes | 0 bytes/s, done.
    Total 3 (delta 0), reused 0 (delta 0)
    To /tmp/repo1
     * [new branch]      master -> master
    ruby-2.2.0 in /tmp/repo2 on master ♥ git log
    commit 1f1fb96deaaf15fa33f1682d87d85014b59b36db
    Author: Alexey Shein <alexey.shein.dev@gmail.com>
    Date:   Tue Sep 22 01:22:12 2015 +0500
    
        First commit
    ruby-2.2.0 in /tmp/repo2 on master ♥ cd ..
    ruby-2.2.0 in /tmp ♥ cd repo1 
    ruby-2.2.0 in /tmp/repo1 ♥ ls
    HEAD  config  description  hooks  info  objects  refs
    ruby-2.2.0 in /tmp/repo1 ♥ git log
    commit 1f1fb96deaaf15fa33f1682d87d85014b59b36db
    Author: Alexey Shein <alexey.shein.dev@gmail.com>
    Date:   Tue Sep 22 01:22:12 2015 +0500
    
        First commit
    ruby-2.2.0 in /tmp/repo1 ♥