Jump to content

User:Mahhegde/sandbox

From Wikipedia, the free encyclopedia

Rspec::Expectations

[edit]

Rspec[1] is a Behavior Driven Development (BDD[2]) testing framework. Rspec::Expectations lets the programmer to specify the expected outcome of an object in unit testing.

Usage Example

[edit]
class Greet
  def greet
    "Good Morning!"
  end
end

describe Greet do
  context "When testing greet method of Greet" do
    it "should return 'Good Morning!'" do
      good_morning = Greet.new
      expect(good_morning.greet).to eq("Good Morning!")
    end
  end
end

Rspec::Expectation has many methods which helps in verifying the returned result. expect statement takes the argument to be tested and makes the validation based on the qualifiers provided as an argument. The to method is used with expect statement. As the name indicates, to method tests the actual argument against expected argument based on matcher's provided as an argument. In the above example, expect statement uses special Rspec keyword eq matcher. This matcher takes the expected value and passes unit test if actual value is same as expected value. Example test case passes only if the returned value from the actual method call good_morning.greet() is 'Good Morning!', otherwise test fails. In case of failure Rspec throws an error and indicates expected and observed values do not match. The programmer can also use not_to Rspec method for exact opposite matching. expect method was introducued in Rspec version 2.11. Before that should and should_not syntax were used. Latest rspec versions supports both expect and should syntax. But expect is recommended over should[3].

good_morning.greet().should eq("Good Morning!")
good_morning.greet().should_not eq("Good Night")

There are several built in matchers in Rspec which can be used with expect()..to or expect()..not_to to define the expectations on an object[4]. Let's refer expect (actual value).to / expect (actual value).not_to as Expectation.

Built in matchers

[edit]
Matchers Description
eq, eql Evaluates to true if actual and expected object values are same
be, equal Evaluates to true if actual and expected objects refer to the same object
>, >=, <, <= Used to check whether actual value >, >=, <, <= to the expected value
be_empty Used to check whether the collection is empty
match Used to match the actual values using regular expression[5]
be_instance_of Used to check if the actual object is same as expected objects class
respond_to Tests if the actual object responds to the specified method
be_between(minimum_value,

maximum_value).inclusive

Tests if the actual value is between [minim_value, maximum_value]
be_between(minimum_value,

maximum_value).exclusive

Tests if the actual value is between (minim_value, maximum_value)
be true Tests if actual == true
be false Tests if actual == false

MiniTest::Expectations

[edit]

MiniTest is a testing framework which provides a complete set of testing utilities including mocking[6], benchmarking and Behaviour Driven Development. From Ruby 1.9,

it's added as part of Ruby's standard library[7]. MiniTest provides expectation syntax which is similar to Rspec Expectation syntax.

Usage Example

[edit]
require 'minitest/autorun'

class GoodMorning
  def greet
    "Good Morning!"
  end
end

describe GoodMorning do
    it "greet() should return 'Good Morning!'" do
      good_morning = GoodMorning.new
      good_morning.greet.must_equal "Good Morning!"
    end
end

Example shown above is almost similar to the example given in Rspec::Expectations section. Instead of expect().to ..eq() syntax, MiniTest uses must_equal.

But both the frameworks tests the value of actual and expected objects for equality. MiniTest::Expectations provides several inbuilt methods[8].

Built in methods

[edit]
Expectation methods Description
must_equal Evaluates to true if actual and expected object values are same
must_be(operator, expected) Takes operators like :<, :>= etc and returns true if actual operator expected

returns true

must_be_empty Tests whether the object is empty
must_be_kind_of(class) Test fails unless invoking object is of type class passed as an argument or it's super type
must_be_nil Test passes only if the object is nill
must_match Used to compare object values using regular expression
must_respond_to(message) Used to test if the method is present in the class

Above built in methods are positive expectations. MiniTest also provides built in negative expectation methods. In most of the cases we just need to replace must with wont. For example, wont_be, wont_be_empty, wont_match etc.

  1. ^ "About RSpec". rspec.info. Retrieved 2016-09-11.
  2. ^ "Behavior-driven development". Wikipedia, the free encyclopedia. 2016-09-03.
  3. ^ "RSpec's New Expectation Syntax". rspec.info. Retrieved 2016-09-11.
  4. ^ "Built in matchers - RSpec Expectations - RSpec - Relish". www.relishapp.com. Retrieved 2016-09-11.
  5. ^ "Class: Regexp (Ruby 2.2.0)". ruby-doc.org. Retrieved 2016-09-11.
  6. ^ "Mock object". Wikipedia, the free encyclopedia. 2016-08-22.
  7. ^ "Getting Started with Minitest". semaphoreci.com. Retrieved 2016-09-11.
  8. ^ "Module: MiniTest::Expectations (Ruby 2.1.0)". ruby-doc.org. Retrieved 2016-09-11.