Hatena::ブログ(Diary)

PerlerのRuby日記

2013年04月11日

[]RSpecのbeforeはeachよりallが先に動く

はまったのでメモ。

beforeがどこに書かれているかに関わらず、eachよりもallの方が先に動くらしい。


spec_helper.rb

require "rspec"

RSpec.configure do |config|
  config.before(:each) do
    puts "config before each"
  end
end

shared_context "sharecon" do
  before(:all) do
    puts "sharecon before all"
  end
end

test_spec.rb

require "spec_helper"

describe "test" do
  context "test" do
    include_context "sharecon"
    it {
      1.should == 1
    }
  end
end
sharecon before all
config before each
.

Finished in 0.00031 seconds
1 example, 0 failures

config.before(:each)の中でTimecop使ってfreezeしてたのに、

shared_contextの中で作ったDBのcreated_atがテストを動かした時間になってて、なんでかなーって調べてわかった。

2013年03月23日

[]三項演算子の代わりに

簡単に書けるならハッシュを使ってる。

slimテンプレート

li class:"#{@sidebar==:home ?'active':nil}" home
li class:"#{{home:'active'}[@sidebar]}" home

@sidebarが:homeなら、

<li class="active">

になるし、

@sidebarが:home以外なら、ハッシュのキーが存在しないから、nil.to_sで

<li class="">

になる。

2013年03月20日

[]cucumber-railsでpendingがCIで失敗扱いになった話

メモ。

cucumber-railsを入れて、

rails generate cucumber:install

したときにできるconfig/cucumber.ymlが

<%                                                                                                                                     
rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
rerun_opts = rerun.to_s.strip.empty? ? "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'} features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~@wip"
%>
default: <%= std_opts %> features
wip: --tags @wip:3 --wip features
rerun: <%= rerun_opts %> --format rerun --out rerun.txt --strict --tags ~@wip

となって 単に bundle exec cucumber などとするとdefaultプロファイルが使われて、--strict も漏れなく付いてきてしまう。


https://github.com/cucumber/cucumber/wiki/Step-Definitions#pending-steps

--strictがついていると、pendingにしているfeatureがあった時点で、

テスト起動コマンド自体の返り値が0でない値*1が返ってきて、Jenkins氏はこのテストを失敗と見なしてしまう。


なので、ここは素直にCI用のプロファイルを作るのが正しいのではないでしょうか。

jenkins: --format pretty --format junit --out features/reports --tags ~@wip

*1Kernel.exit(1) if failure

2013年03月01日

[]二次元配列は1回のeachで回せる

教えてもらったのでメモランダム。


いままで二次元配列は二回eachを使って取り出していたが、

ブロック引数に複数指定すると中の配列の要素がそのまま取り出せる。

いままでやってたやり方

array = [[1,2,3], [4,5,6], [7,8,9]]
array.each do |ary|
  ary.each do |elm|
    puts elm
  end
end

今日覚えたやり方

array = [[1,2,3], [4,5,6], [7,8,9]]
array.each do |elm1, elm2, elm3|
  puts elm1, elm2, elm3
end

なるほどなー。

2013年02月25日

[]Rails勉強会@東京 第80回に行ってきた

Rails勉強会@東京 第80回 - Rails勉強会@東京

  • 初めてのRails勉強会に行ってきた
  • 入口の裏口に@onkさんと@nobyukiさんがいて戸惑うことなく入館できたよかった
  • 自分含め5人で少ないなと思ってたら、結局そのまま5人だけだった
  • やったこと

その他

  • 御徒町は客引きのひとがいっぱいいてちょっと怖かった
  • やっぱり5人は少ないと思った
  • Facebookアカウント持ってないので、多少の疎外感
  • やっぱり当日にやることを決めると、gdgdしてしまうところがあった
  • プロンプトのゆのっちが気になってしょうがなかった