24
Chiew Chung @theworldinunion Edward Middleton @e14n

RSpec 1.x -> 2.0 の変更点

Embed Size (px)

DESCRIPTION

RSpec 1.x -> 2.0 の変更点

Citation preview

Page 1: RSpec 1.x -> 2.0 の変更点

Chiew Chung @theworldinunion Edward Middleton @e14n

Page 2: RSpec 1.x -> 2.0 の変更点

# 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

Page 3: RSpec 1.x -> 2.0 の変更点

$ 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

Page 4: RSpec 1.x -> 2.0 の変更点

# 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

Page 5: RSpec 1.x -> 2.0 の変更点
Page 6: RSpec 1.x -> 2.0 の変更点

Rspec-core (ランナー、フォーマッター,…)

Rspec-expectations (Should, matchers,…)

Rspec-mocks (mock, stub,…)

Rspec Rspec-Rails

Page 7: RSpec 1.x -> 2.0 の変更点
Page 8: RSpec 1.x -> 2.0 の変更点

rspec ./spec Rspec 2.0 “rspec”

spec _1.3.1_ ./spec Rspec 1.0 “spec”

Page 9: RSpec 1.x -> 2.0 の変更点

RSpec::Core::RakeTask.new(:rcov) do |t| t.rcov_opts = %q[--exclude "spec"] end

'rspec/core/rake_task'

Page 10: RSpec 1.x -> 2.0 の変更点

bundle exec autotest

もしくはbundler使ってる場合

rspec ‒configure autotest

./autotest/discover.rbを作成される

autotest

Page 11: RSpec 1.x -> 2.0 の変更点

RSpec.configure do |c| # ....

end

spec/spec_helper.rbの設定ファイル

command line ./.rspec ~/.rspec

優先順:

Page 12: RSpec 1.x -> 2.0 の変更点

describe "something" do context "in some context" do it "does something" do # ... end end end

もちろんnestedの場合は問題ないです

Page 13: RSpec 1.x -> 2.0 の変更点
Page 14: RSpec 1.x -> 2.0 の変更点
Page 15: RSpec 1.x -> 2.0 の変更点

# 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

Page 16: RSpec 1.x -> 2.0 の変更点

RSpec.configure do |c| c.filter_run :focus => true c.run_all_when_everything_filtered = true end

:focus => trueのテストは一個もないときに、全てのテストを走らせる。

すなわち、時々いくつかのテストに”focus”したいときに、”focus”のタグ を追加するだけで、”focus”のタグを全部外したら、何も変更せずまた 全てのテストを走らせてくれる。

Page 17: RSpec 1.x -> 2.0 の変更点

# 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のテストが違ってくるとき

Page 18: RSpec 1.x -> 2.0 の変更点
Page 19: RSpec 1.x -> 2.0 の変更点
Page 20: RSpec 1.x -> 2.0 の変更点

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のみ

Page 21: RSpec 1.x -> 2.0 の変更点
Page 22: RSpec 1.x -> 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

Page 23: RSpec 1.x -> 2.0 の変更点

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を使おう

Page 24: RSpec 1.x -> 2.0 の変更点