LoginSignup
4

More than 5 years have passed since last update.

wercker を試してみる

Posted at

wercker は、無料で使えるCIサービスで、Github、Bitbucket に対応している。

wercker - automation driven development : http://wercker.com/

今回は試しに、FizzBuzz をお題にやってみる。

7kaji/fizzbuzz : https://github.com/7kaji/fizzbuzz

RSpec

fizzbuzz_spec.rb
require 'rspec'
require 'fuubar'

# ファイルを読み込む(定数の初期化)
require './fizzbuzz'

#FizzBuzz 与えられた数字が15のとき
describe FizzBuzz do
  # テストする対象を明示する
  subject  { FizzBuzz.new }
  # context でテストケースの状況を指定する
  context "与えられた数字が3の倍数でも5の倍数でもないとき" do
    # itメソッドが1つのテストケースになる
    it "与えられた数字を返すこと" do
      expect(subject.say(1)).to eq 1
    end
  end
  context "与えられた数字が3の倍数の時" do
    it { expect(subject.say(3)).to eq 'Fizz' }
  end
  context "与えられた数字が5の倍数の時" do
    it { expect(subject.say(5)).to eq 'Buzz' }
  end
  context "与えられた数字が3かつ5の倍数の時" do
    it { expect(subject.say(15)).to eq 'FizzBuzz' }
  end
end

クラス定義だけかいて、rspec 実行(ローカル)。

fizzbuzz.rb
#!/user/local/bin/ruby

# FizzBuzz
class FizzBuzz

end
rspec fizzbuzz_spec.rb

  1) FizzBuzz 与えられた数字が3の倍数でも5の倍数でもないとき 与えられた数字を返すこと
     Failure/Error: expect(subject.say(1)).to eq 1
     NoMethodError:
       undefined method `say' for #<FizzBuzz:0x007fa8a3072b48>
     # ./fizzbuzz_spec.rb:15:in `block (3 levels) in <top (required)>'


  2) FizzBuzz 与えられた数字が3の倍数の時
     Failure/Error: it { expect(subject.say(3)).to eq 'Fizz' }
     NoMethodError:
       undefined method `say' for #<FizzBuzz:0x007fa8a306a240>
     # ./fizzbuzz_spec.rb:19:in `block (3 levels) in <top (required)>'


  3) FizzBuzz 与えられた数字が5の倍数の時
     Failure/Error: it { expect(subject.say(5)).to eq 'Buzz' }
     NoMethodError:
       undefined method `say' for #<FizzBuzz:0x007fa8a3061b68>
     # ./fizzbuzz_spec.rb:22:in `block (3 levels) in <top (required)>'


  4) FizzBuzz 与えられた数字が3かつ5の倍数の時
     Failure/Error: it { expect(subject.say(15)).to eq 'FizzBuzz' }
     NoMethodError:
       undefined method `say' for #<FizzBuzz:0x007fa8a30587c0>
     # ./fizzbuzz_spec.rb:25:in `block (3 levels) in <top (required)>'

 4/4 |=============================================== 100 ================================================>| Time: 00:00:00

Finished in 0.00323 seconds (files took 0.14819 seconds to load)
4 examples, 4 failures

Failed examples:

rspec ./fizzbuzz_spec.rb:14 # FizzBuzz 与えられた数字が3の倍数でも5の倍数でもないとき 与えられた数字を返すこと
rspec ./fizzbuzz_spec.rb:19 # FizzBuzz 与えられた数字が3の倍数の時
rspec ./fizzbuzz_spec.rb:22 # FizzBuzz 与えられた数字が5の倍数の時
rspec ./fizzbuzz_spec.rb:25 # FizzBuzz 与えられた数字が3かつ5の倍数の時

はい、失敗。

wercker でも試す

  • Github にリポジトリ作成
  • wercker でも New application して連携させる(ポチポチ)
  • wercker.yml をレポジトリ直下に配置
wercker.yml
box: wercker/ruby
build:
    steps:
        - bundle-install
        - script:
            name: echo ruby information
            code: |
                echo "ruby version $(ruby --version) running!"
                echo "from location $(which ruby)"
                echo -p "gem list: $(gem list)"
        - script:
            name: Run RSpec
            code: bundle exec rspec fizzbuzz_spec.rb

wercker.yml の設定は以下の記事がくわしい。

Werckerの仕組み,独自のboxとstepのつくりかた | SOTA : http://deeeet.com/writing/2014/10/16/wercker/

git 設定

$ git init
Initialized empty Git repository in /Users/white/Dev/fizzbuzz/.git/
$ git add .
$ git commit -m 'first commit'
[master (root-commit) 3e30faa] first commit
 8 files changed, 118 insertions(+)
 create mode 100644 .rspec
 create mode 100644 .wercker
 create mode 100644 Gemfile
 create mode 100644 Gemfile.lock
 create mode 100644 README.md
 create mode 100644 fizzbuzz.rb
 create mode 100644 fizzbuzz_spec.rb
 create mode 100644 wercker.yml
$ git remote add origin git@github.com:7kaji/fizzbuzz.git
$ git remote -v
origin  git@github.com:7kaji/fizzbuzz.git (fetch)
origin  git@github.com:7kaji/fizzbuzz.git (push)
$ git push -u origin master
Counting objects: 10, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (10/10), 1.71 KiB | 0 bytes/s, done.
Total 10 (delta 0), reused 0 (delta 0)
To git@github.com:7kaji/fizzbuzz.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

Github に push すると、wercker の方で rspec まわしてくれる(結果はメールでもくる)。
スクリーンショット 2015-04-12 17.38.45.png

rspec の failed の内容も出力される

FizzBuzz 実装

fizzbuzz.rb
#!/user/local/bin/ruby

# FizzBuzz
class FizzBuzz
  def say(n)
    return 'FizzBuzz' if fizzbuzz? n
    return 'Fizz' if fizz? n
    return 'Buzz' if buzz? n
    n
  end

  private

  def fizzbuzz?(n)
    fizz?(n) && buzz?(n)
  end

  def fizz?(n)
    n % 3 == 0
  end

  def buzz?(n)
    n % 5 == 0
  end
end

もう一度 push !!

スクリーンショット 2015-04-12 17.45.37.png

rspec 通ったー。

Badge をみせておく

README などに結果の Badge をのせる。

スクリーンショット 2015-04-12 17.48.10.png

コピペでREADME.md に追加。

今回は、テストを回しただけだけど、deploy したり、他にもたくさん機能があるようです。
無料だし、さいこう。

ちな

あとで、みてみよう。

REF

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4