Friday, November 28, 2014

Difference Between AreSame and AreEqual

==================
1.Assert.AreSame Method
========================
Verifies that specified object variables refer to the same objects.
http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.assert.aresame(v=VS.100).aspx

2.Assert.AreEqual Method
============================
Verifies that specified values are equal.
http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.assert.areequal(v=VS.100).aspx

List of test methods:
==============
1. Passed.
[TestMethod]
public void IsStringEqualTest
{
const string INPUT = "Obama";
const string EXPEXTED = "Obama"
Assert.AreEqual(EXPEXTED, INPUT);
}

2. Failed
.[TestMethod]
public void IsStringEqualNoIgnoreCaseTest
{
const string INPUT = "Obama";
const string EXPEXTED = "obama"
Assert.AreEqual(EXPEXTED, INPUT);
}

3. Passed.
[TestMethod]
public void IsStringEqualWithIgnoreCaseTest()
{
const string INPUT = "Obama";
const string EXPEXTED = "obama"
Assert.AreEqual(EXPEXTED, INPUT, true);
}

4. Failed
// Please use Assert.AreSame for only ref types.
// For value type useAssert.AreEqual !!!
[TestMethod]
public void IntegerSameTest()
{
int INPUT = 100;
int EXPEXTED = 100;
Assert.AreSame(EXPEXTED, INPUT);
}

5.Passed
.[TestMethod]
public void IntegerSameTest()
{
int INPUT = 100;
int EXPEXTED = 100;
Assert.AreEqual(EXPEXTED, INPUT);
}

Conclution:
===============================
Use Assert.AreSame for only ref types.
For value type use Assert.AreEqual only.
===============================

Monday, November 17, 2014

Mock Object vs. Stub Object


Mock Objects


A mock object is a simulation of a real object. Mock objects act just as real objects but in a controlled way. A mock object is created to test the behavior of a real object. In unit testing, mock objects are used to scrutinize the performance of real objects. Simply said, a mock object is just the imitation of a real object. Some important characteristics of mock objects are that they are lightweight, easily created, quick and deterministic, and so on.

Stub Object

A stub object is an object which implements an interface of a component. A stub can be configured to return a value as required. Stub objects provide a valid response, but it is of static nature meaning that no matter what input is passed in, we always get the same response.

Mock Object vs. Stub Object


Mock objects vary from stub objects in some ways. They are listed below:
The first distinct factor is that mock objects test themselves, i.e., they check if they are called at the proper time in the proper manner by the object being tested. Stubs generally just return stubbed data, which can be configured to change depending on how they are called.
Secondly, mock objects are generally lightweight relative to stubs with different instances set for mock objects for every test whilst stubs are often reused between tests.

Followers

Link