BDD RSpec describe() it()

名词解释code example:

    An executable example of how the subject code can be
    used, and its expected behaviour (expressed with expectations) in
    a given context. In BDD, we write the code examples before the
    subject code they document.
名词解释:example group
    A group of code examples.
名词解释:subject code
    The code whose behaviour we are specifying with RSpec.

RSpec provides a Domain Specific Language for specifying the behaviour
of objects. It embraces the metaphor of describing behaviour the way we
might express it if we were talking to a customer, or another developer.
A snippet of such a conversation might look like this:
You: Describe a new account
Somebody else: It should have a balance of zero
Here’s that same conversation expressed in RSpec:
describe "A new Account" do
  it "should have a balance of 0" do
    account = Account.new
    account.balance.should == Money.new(0, :USD)
  end
end
We use the describe( ) method to define an example group. The string we
pass to it represents the facet of the system that we want to describe
(a new account). The block holds the code examples that make up that
group.


The it( ) method defines a code example. The string passed to it describes
the specific behaviour we’re interested in specifying about that facet
(should have a balance of zero). The block holds the example code that
exercises the subject code and sets expectations about its behaviour.
Using strings like this instead of legal Ruby class names and method
names provides a lot of flexibility. Here’s an example from RSpec’s own
code examples:
it "should match when value < (target + delta)" do
  be_close(5.0, 0.5).matches?(5.49).should be_true
end

你可能感兴趣的:(Ruby,UP,rspec)