User:Aagrawa6/sandbox

An expectation is the most expected outcome of an event in case of uncertainty. In Ruby, there are various packages such as RSpec and  respectively. With Minitest, four types of expectations can be defined: unary expectations , binary expectations , reverse expectations and block/proc expectations . Mocha defines a traditional mock object. Calling a method on this object, it searches through its expectations from the newest expectation to the oldest expectation to find the one that matches the invocation. An  expectation only matches once and is ignored on future calls while an   expectation is matched upon every invocation.

RSpec::Expectations
RSpec is a Behavior Driven Development (BDD ) testing framework. RSpec::Expectations lets the programmer specify the expected outcome of an object in a test case.

Usage Examples
RSpec::Expectation has many methods which helps in verifying the returned result. The  statement takes the argument to be tested and makes the validation based on the qualifiers provided as an argument. The  method is used with   statement. As the name indicates,  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  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  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  RSpec method for exact opposite matching.

method was introducued in RSpec version 2.11. Before that  and   syntax were used. Latest RSpec versions support both  and   syntax. But  is recommended over. There are several built in matchers in RSpec which can be used with  define the expectations on an object.

Mocha::Expectations
Mocha is a Ruby library for mocking and stubbing. It provides a readable, unified and simple syntax for partial as well as full mocking. It is also supported by many other frameworks. A stub is a type of expectation of zero or more invocations. These are defined to make the intent of the test more explicit. Mocha defines a traditional mock object. The mock object searches through its expectations from newest to oldest to find one that matches the invocation.

Instance Methods Available
All methods defined on Mock#expects, Mock#stubs, ObjectMethods#expects and ObjectMethods#stubs returns an Expectation. Various Instance methods available are:
 * 1) (Expectation) at_least(minimum_number_of_times)
 * 2) (Expectation) at_least_once
 * 3) (Expectation) at_most(maximum_number_of_times)
 * 4) (Expectation) at_most_once
 * 5) (Expectation) in_sequence(*sequences)
 * 6) (Expectation) multiple_yields(*parameter_groups)
 * 7) (Expectation) never
 * 8) (Expectation) once
 * 9) (Expectation) raises(exception = RuntimeError, message = nil)
 * 10) (Expectation) returns(*values)
 * 11) (Expectation) then(*parameters)
 * 12) (Expectation) throws(tag, object = nil)
 * 13) (Expectation) times(range)
 * 14) (Expectation) twice
 * 15) (Expectation) when(state_predicate)
 * 16) (Expectation) with(*expected_parameters) {|actual_parameters| ... }
 * 17) (Expectation) yields(*parameters)

MiniTest::Expectations
MiniTest is a testing framework which provides a complete set of testing utilities including mocking, benchmarking and Behaviour Driven Development. From Ruby 1.9, it's added as part of Ruby's standard library. MiniTest provides expectation syntax which is similar to Rspec's Expectation syntax.

Usage Example
Example shown above is almost similar to the example given in RSpec::Expectations section. Instead of  syntax, MiniTest uses

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

Built in methods
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 ' with    For example,   etc.

Videos

 * 1) Stubbing and Mocking in RSpec
 * 2) Stubbing and Mocking with RSpec - The codeship
 * 3) RSpec - Should and Expect
 * 4) Understanding Mock Objects
 * 5) Getting Started with Minitest Rails