访问次数
我的另一个总结性博客: todototry 大米粒

使用Rspec进行行为驱动测试

使用Rspec进行行为驱动测试 : 

require 'machine' 

describe Machine do #Machine 是要测试的类名
before :each do
@machine
=Machine.new([:shopping,:checking_out])
@machine.events
= {:checkout =>{:from => :shopping, :to => :checking_out}}
end

it
"should initially have a state of the first state" do
@machine.state.should
== :shopping
end

it
"should remember a list of valid states" do
@machine.states.should
== [:shopping, :checking_out]
end

it
"should remember a list of events with transitions" do
@machine.events.should
== {:checkout => {:from => :shopping, :to => :checking_out}}
end


it
"should remember a list of valid states" do
@machine.states
= [:shopping, :checking_out]
@machine.states.should
== [:shopping,:checking_out]
end

it
"should transition to :checking_out upon #trigger(:checkout) event " do
@machine.trigger(:checkout)
@machine.state.should
== :checking_out
end

it
"should transition to :success upon #trigger(:accept_card)" do
@machine.events
= {
:checkout
=>
{:
from => :shopping,:to => :checking_out},

:accept_card
=>
{:
from => :checking_out,:to => :success}
}
@machine.trigger(:checkout)
@machine.state.should
== :checking_out
@machine.trigger(:accept_card)
@machine.state.should
== :success
end


it
"should not transition to :success upon #trigger(:accept_card)" do
@machine.events
= {
:checkout
=>
{:
from => :shopping,:to => :checking_out},

:accept_card
=>
{:
from => :checking_out,:to => :success}
}
@machine.trigger(:accept_card)
@machine.state.should_not
== :success
end
end

以下是ruby code:  

class Machine

attr_accessor :state ,:events,:states

def initialize(states)
@states
= states
@state
= @states[0]
end

def trigger(event)
@state
= events[event][:to] if state== events[event][:from]
end

end

首先学习Rspec必须先了解测试驱动开发(TDD) 和 行为驱动开发(BDD)。

TDD:利用测试来驱动软件的设计和开发,是极限编程的重要特点,方法主要是,先写测试程序,然后再通过编码使其通过测试,他以不断的测试驱动代码的开发,这样即简化了代码又保证来软件的质量。

原理:

【图 X测试模型】

BDD:

RSpec 是一门专门用于描述Ruby程序的行为的ruby域指定语言。RSpec让应用程序的作者能以流畅的语言来表达他们的额设计意图。

Rspec是一系列行为的集合。这些行为具有对应的实例。

下面简要介绍Rspec 使用

1.should和预期情况

@variable.should be valid

@var.should == expected

@var.should ===expected

@var.should =~ regexp

2.结果预测

target.should be_true

target.should be_nil

target.should_not be_nil

target.should be_empty

target.should be_an_instance_of(Fixnum)

target.should be_a_kind_of(Fixnum)

target.should have_key(:key)

{:foo=>"foo"}.should have_key(:foo)

[1,2,3,4,5].should include(1)

[1,2,3,4,5].should have(5).items

3.支持自定义的断言方法

    实现步骤:

  一:定义类 class WhatEver,

  二:实现

  1. initialize(expected)

  2.matches?(actual)

  3.failure_message

  4.negative_failure_message

  5.description

  三:

  在你的spec所包含的模组中,加入

  def be_finished(expected)

     WhatEver.new(expected)

  end

  四:调用

   bob.should be_finished(x)

4.共享的行为

    1.before(:all)

    2.before(:each)

    3.after(:each)

    4.after(:all)

    5.所有预期的情况

    6.各种包含入的模组

可以通过:shared=>true 来定义共享行为

---待续

 5.RSpec的数据模拟和占位代码

---待续

posted @ 2011-07-29 23:03  fandyst  阅读(462)  评论(0编辑  收藏  举报