`
yang_kunlun
  • 浏览: 77284 次
  • 性别: Icon_minigender_1
  • 来自: 地球
最近访客 更多访客>>
社区版块
存档分类
最新评论

Outline of Rspec

    博客分类:
  • Test
阅读更多
1. Describe block

describe "describes" do
   before(:each) do
     this will execute every time
   end
   it “it describes” do
    should
    should_not
   end
end


2. Should and should_not method

receiver.should(matcher)                # the simplest example
# Passes if matcher.matches?(receiver)
receiver.should == expected #any value
# Passes if (receiver == expected)
receiver.should === expected #any value
# Passes if (receiver === expected)
receiver.should =~ regexp
# Passes if (receiver =~ regexp)


alse can use

target.should be_true
target.should be_false
target.should be_nil
target.should_not be_nil
3.should be_a_kind_of(Fixnum)
3.should be_an_instance_of(Fixnum)
{:foo => “foo”}.should have_key(:foo)
[1, 2, 3].should include(1)
[1, 2, 3].should have(3).items


3. Shared Behaviors

• before(:all)
• before(:each)
• after(:each)
• after(:all)

describe “people in general”
  before(:each) do
    puts “shared before()”
  end
  after(:each) do
    puts “shared after()”
  end
  ...
end
describe Teacher do
  before(:each) do
    puts “teacher before()”
    @person = Teacher.new(“Ms. Smith”, 30, 50000)
  end
  after(:each) do
    puts “teacher after()”
  end
  it_should_behave_like “people in general”
  ...
end


it_should_behave_like “people in general” can shared methods in people in general. Cool! 4. RSpec’s Mocks and Stubs
Mock Objects

echo.should_receive(:sound).with(“hey”).and_return(“hey”)


Stub Objects

yodeler = stub(‘yodeler’, :yodels? => true)


5. Running Specs

$ spec spec/models/credit_card_spec.rb
$ spec -fs spec/models/credit_card_spec.rb
$ spec -fr spec/models/authorization_spec.rb


6. The RSpec on Rails Plugin

$ script/generate rspec
$ script/generate rspec_model Schedule name:string
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics