Upload
theworldinunion
View
4.310
Download
2
Embed Size (px)
DESCRIPTION
RSpec 1.x -> 2.0 の変更点
Citation preview
Chiew Chung @theworldinunion Edward Middleton @e14n
# in spec/calculator_spec.rb describe Calculator, "add" do it "returns the sum of its arguments" do Calculator.new.add(1, 2).should eq(3) end end
$ rspec spec/calculator_spec.rb ./spec/calculator_spec.rb:1: uninitialized constant Calculator
# in lib/calculator.rb class Calculator def add(a,b) a + b end end
# in spec/calculator_spec.rb require “calculator” describe Calculator, "add" do it "returns the sum of its arguments" do Calculator.new.add(1, 2).should eq(3) end end
$ rspec spec/calculator_spec.rb .
Finished in 0.000315 seconds 1 example, 0 failures
Rspec-core (ランナー、フォーマッター,…)
Rspec-expectations (Should, matchers,…)
Rspec-mocks (mock, stub,…)
Rspec Rspec-Rails
rspec ./spec Rspec 2.0 “rspec”
spec _1.3.1_ ./spec Rspec 1.0 “spec”
RSpec::Core::RakeTask.new(:rcov) do |t| t.rcov_opts = %q[--exclude "spec"] end
'rspec/core/rake_task'
bundle exec autotest
もしくはbundler使ってる場合
rspec ‒configure autotest
./autotest/discover.rbを作成される
autotest
RSpec.configure do |c| # ....
end
spec/spec_helper.rbの設定ファイル
command line ./.rspec ~/.rspec
優先順:
describe "something" do context "in some context" do it "does something" do # ... end end end
もちろんnestedの場合は問題ないです
# in spec/spec_helper.rb RSpec.configure do |c| c.filter_run :focus => true end
# in any spec file describe "something" do it "does something", :focus => true do # .... end end
RSpec.configure do |c| c.filter_run :focus => true c.run_all_when_everything_filtered = true end
:focus => trueのテストは一個もないときに、全てのテストを走らせる。
すなわち、時々いくつかのテストに”focus”したいときに、”focus”のタグ を追加するだけで、”focus”のタグを全部外したら、何も変更せずまた 全てのテストを走らせてくれる。
# in spec/spec_helper.rb RSpec.configure do |c| c.exclusion_filter = { :ruby => lambda {|version| !(RUBY_VERSION.to_s =~ /^#{version.to_s}/) }} end
# in any spec file describe "something" do it "does something", :ruby => 1.8 do # .... end
it "does something", :ruby => 1.9 do # .... end end
たとえば、Ruby 1.8 と Ruby 1.9のテストが違ってくるとき
actual.should == expected # object equality actual.should equal(expected) # object identity
Rspec 1.x , RSpec 2.0
actual.should eq(expected) # object equality actual.should be(expected) # object identity
RSpec 2.0のみ
def eat_cheese simple_matcher("eat cheese") do |actual| actual.eat?(:cheese) end end
旧
RSpec::Matchers.define :eat_cheese do match do |actual| actual.eat?(:cheese) end end
新
RSpec::Matchers.define :eat_cheese do match do |actual| actual.should eat?(:cheese) end end
RSpec::Matchers.define :eat_cheese do include MyCheesyAssertions match_unless_raises Test::Unit::AssertionFailedError do |actual| assert_eats_chesse actual end end
Rspec::Matchers.defineを使おう