Use rspec for behavior-Driven Testing

Source: Internet
Author: User

Use rspec for behavior-driven testing:

Require 'machine'

Describe machine do # machine is the name of the class to be tested
Before: each do
@ Machine = machine. New ([: shopping,: checking_out])
@ Machine. Events = {: checkout = >{: from =>: shopping,: To =>: checking_out }}
End

It "shoshould initially have a State of the first State" do
@ Machine. state. shocould =: Shopping
End

It "shoshould remember a list of valid states" do
@ Machine. States. shold = [: shopping,: checking_out]
End

It "shocould remember a list of events with transitions" do
@ Machine. Events. shocould ={: checkout =>{: from =>: shopping,: To =>: checking_out }}
End


It "shoshould remember a list of valid states" do
@ Machine. States = [: shopping,: checking_out]
@ Machine. States. shold = [: shopping,: checking_out]
End

It "shoshould transition to: checking_out upon # Trigger (: checkout) event" do
@ Machine. Trigger (: checkout)
@ Machine. state. shocould =: checking_out
End

It "shoshould 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. shocould =: checking_out
@ Machine. Trigger (: accept_card)
@ Machine. state. shocould =: Success
End


It "shocould 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

The following is the 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

To learn rspec, you must first understand test-driven development (TDD) and behavior-driven development (BDD ).

TDD: the use of testing to drive the design and development of software is an important feature of extreme programming. The main method is to first write the test program and then pass the test by coding, he continuously tests the code development, which simplifies the code and guarantees the quality of the software.

Principle:

BDD:

Rspec is a ruby domain-specific language specifically used to describe the behavior of Ruby programs. Rspec allows application authors to express their design intent in a fluent language.

Rspec is a collection of behaviors. These actions have corresponding instances.

The following describes how to use rspec.

1. shocould and expectations

@ Variable. shocould be valid

@ Var. shoshould = expected

@ Var. shoshould = expected

@ Var. shoshould = ~ Regexp

2. Result Prediction

Target. shoshould be_true

Target. shoshould be_nil

Target. should_not be_nil

Target. shoshould be_empty

Target. shoshould be_an_instance_of (fixnum)

Target. shoshould be_a_kind_of (fixnum)

Target. shoshould have_key (: key)

{: Foo => "foo"}. shocould have_key (: Foo)

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

[1, 2, 3, 4, 5]. shoshould have (5). Items

3. Support for custom assertions

Steps:

I. Define class whatever,

II. Implementation

1. initialize (expected)

2. matches? (Actual)

3. failure_message

4. negative_failure_message

5. Description

III:

In the modules included in your spec, add

Def be_finished (expected)

Whatever. New (expected)

End

Iv. Call

Bob. shoshould be_finished (X)

4. Shared Behavior

1. Before (: All)

2. Before (: each)

3. After (: each)

4. After (: All)

5. All expected situations

6. Various included modules

You can use: Shared => true to define the sharing behavior.

--- To be continued

5. rspec data simulation and placeholder code

--- To be continued

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.