Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Rhino.Mocks;
using Rhino.Mocks.Constraints;
using BasicApp.App.Utilities;
namespace BasicApp.Test.UtilitiesUnderTest
{
[TestFixture]
class ProfessionUtilitiesUnderTest
{
private UtilitiesProfession _ObjUtil = null;
[SetUp]
public void Setup()
{
_ObjUtil = new UtilitiesProfession();
}
[Test]
public void GetCompleteProfession_Return_SheIsASoftwareEngineer()
{
string StrResult = _ObjUtil.GetCompleteProfession("software engineer", UtilitiesProfession.Gender.Female);
StringAssert.AreEqualIgnoringCase("She is a software engineer", StrResult);
}
[Test]
public void GetCompleteProfession_Return_HeIsAProjectManager()
{
string StrResult = _ObjUtil.GetCompleteProfession("software engineer", UtilitiesProfession.Gender.Male);
StringAssert.AreEqualIgnoringCase("He is a software engineer", StrResult);
}
[Test]
public void GetCompleteProfession_Return_HeIsAnEngineer()
{
string StrResult = _ObjUtil.GetCompleteProfession("engineer", UtilitiesProfession.Gender.Male);
StringAssert.AreEqualIgnoringCase("He is an engineer", StrResult);
}
[TearDown]
public void TearDown()
{
_ObjUtil = null;
}
}
[TestFixture]
class DateUtilitiesUnderTest
{
private MockRepository _Mocks = null;
private UtilitiesDate _ObjUtil = null;
private TestableUtilitiesDate _ObjUtilTestable = null;
private StubReader _Reader = null;
private string[] _ExpectedMonths = null;
private string _ExpectedMonthsAsString = null;
[SetUp]
public void Setup()
{
_Mocks = new MockRepository();
_ObjUtil = new UtilitiesDate();
_ObjUtilTestable = new TestableUtilitiesDate();
_Reader = new StubReader();
_ExpectedMonths =
new string[12]
{"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
};
_ExpectedMonthsAsString
= string.Join("\r\n", _ExpectedMonths);
}
[Test]
public void GetWeeks_Return_6()
{
decimal Weeks = _ObjUtil.GetWeeks(DateTime.Now.AddDays(-42), DateTime.Now);
Assert.AreEqual(6, Weeks);
}
[Test]
public void GetDays_Return_25()
{
decimal Days = _ObjUtil.GetDays(DateTime.Now.AddDays(-25), DateTime.Now);
Assert.AreEqual(25, Days);
}
[Test]
public void GetMonths_Returns_All_Months_InjectStub_ByParamPass()
{
string [] Months = _ObjUtil.GetMonthsStubByParamInject(_Reader);
Assert.AreEqual(_ExpectedMonths, Months);
}
[Test]
public void GetMonths_Returns_All_Months_InjectStub_ByConstuctor()
{
_ObjUtil = new UtilitiesDate(_Reader);
string[] Months = _ObjUtil.GetMonthsStubByConstructorInject();
Assert.AreEqual(_ExpectedMonths, Months);
}
[Test]
public void GetMonths_Returns_All_Months_InjectStub_ByPropertyInject()
{
_ObjUtil.Reader = _Reader;
string[] Months = _ObjUtil.GetMonthsStubByPropertyInject();
Assert.AreEqual(_ExpectedMonths, Months);
}
[Test]
public void GetMonths_Returns_All_Months_InjectStub_UsingFactory()
{
ReaderFactory.SetReader(_Reader);
string[] Months = _ObjUtil.GetMonthsStubUsingFactory();
Assert.AreEqual(_ExpectedMonths, Months);
}
[Test]
public void GetMonths_Returns_All_Months_InjectStub_UsingLocalFactory()
{
TestableUtilitiesDate ObjUtil = new TestableUtilitiesDate();
_ObjUtilTestable._Reader = _Reader;
string[] Months = _ObjUtilTestable.GetMonthsStubUsingFactory();
Assert.AreEqual(_ExpectedMonths, Months);
}
/* MANUALLY WRITTEN MOCKS
*
* Mocks, like stubs, need to be injected into the code. The very same strategies that were used for stubs
* can be used with mocks. Parameter, Constructor, Property, Factory, and Local Factory are all valid. Here
* is a test using manually written mocks. Here I use the local factory method.
*/
[Test]
public void SetMonths_Proper_Content_Sent()
{
MockWriter Writer = new MockWriter();
_ObjUtilTestable._Writer = Writer;
_ObjUtilTestable.SetMonths("emptyFile.txt");
Assert.AreEqual(_ExpectedMonthsAsString,Writer._Content);
}
/* Isolation Frameworks are a great tool for saving time on having to manually create stubs and mocks.
* While they are rich in functionality it's still up to the programmer where mocks and stubs need to placed
* in the code. Poor placement of seams can lead to messy code and tests that are hard to understand.
*/
/* STRICT MOCKS
*
* Strict mock objects require that the set expectations be met completely for
* the test to pass. This means that if any recorded call differs in either parameters
* or name the test will fail immediately. It won't wait till verify is called. One
* thing to note here is that because ValidateAndWrite is expected to return something
* we need to specify in our test code what will be returned after that line. Remember that
* writer is an empty shell!
*
* Means of Failure:
* -an unexpected method is called
* -an expected method is never called
*/
[Test]
public void RhinoMock_SetMonths_Proper_Content_Sent_Strict()
{
// initialization
IWriter Writer = _Mocks.StrictMock<IWriter>();
_ObjUtilTestable._Writer = Writer;
// expecatation
using (_Mocks.Record())
{
Writer.ValidateAndWrite("emptyFile.txt", _ExpectedMonthsAsString);
LastCall.Return(true);
}
// invokation
_ObjUtilTestable.SetMonths("emptyFile.txt");
// verification
_Mocks.Verify(Writer);
}
/* NON-STRICT MOCKS
*
* Non-Strict mock objects require that the methods set in the expectation be called
* but it also allows other methods to be called. This makes the test less prone to
* failure.
*
* Means of Failure:
* -an expected method is never called
*/
[Test]
public void RhinoMock_SetMonths_Proper_Content_Sent_NonStrict()
{
IWriter Writer = _Mocks.DynamicMock<IWriter>();
_ObjUtilTestable._Writer = Writer;
using (_Mocks.Record())
{
Writer.ValidateAndWrite("emptyFile.txt",_ExpectedMonthsAsString);
LastCall.Return(true);
}
_ObjUtilTestable.SetMonths("emptyFile.txt");
_Mocks.Verify(Writer);
}
/* USE OF STUBS
*
* Don't the let the name Rhino Mock fool you! this framework is capable of creating
* stubs as well as mocks!
*/
[Test]
public void RhinoMock_GetMonths_Returns_All_Months()
{
IReader Reader = _Mocks.Stub<IReader>();
_ObjUtilTestable._Reader = Reader;
// We set expectations purely to tell the stub what to return.
using (_Mocks.Record())
{
Reader.ValidateAndRead("file.txt");
LastCall.Return(_ExpectedMonthsAsString);
}
string[] Result = _ObjUtilTestable.GetMonthsStubUsingLocalFactory();
// If you verify the test will always pass, by definition stubs
// can't cause a test to fail, so we assert on something!
Assert.AreEqual(_ExpectedMonths, Result);
}
/* CONSTRAINTS
*
* We can also test the object properties of our parameters! Check more docs on constraints as
* they offer very rich functionality to the test code by allowing us to set expectations on inputs.
* One thing to note is the use of overloaded AND/OR operators.
*
* Concern: One thing I didn't understand was that in the book "The Art of Unit Testing"
* the author would instantiate new instances of input in both the expectation and invokation, making them
* different. From my expierence it seems like you need to expect a specific instance. If a different
* instance is recieved in the invokation step the test will fail. Does this mean I have to inject my input
* as well as my Writer? For primitive types this wasn't the case.
*
* UPDATE: Now it works like in the book ... (Try and replicate error)
*/
[Test]
public void RhinoMock_SetMonthsUsingWriterInput_Proper_Content_Sent()
{
IWriter Writer = _Mocks.DynamicMock<IWriter>();
_ObjUtilTestable._Writer = Writer;
using (_Mocks.Record())
{
Writer.ValidateAndWrite(new WriterInput("emptyFile.txt", _ExpectedMonthsAsString));
LastCall.Constraints(
Property.Value("Path", "emptyFile.txt")
&& Property.Value("Content", _ExpectedMonthsAsString)
).Return(true);
}
_ObjUtilTestable.SetMonthsUsingWriterInput("emptyFile.txt");
_Mocks.VerifyAll();
}
/* DELEGATES
*
* //// MISSING EXAMPLE ////
*
* Concern: I noticed that certain features are missing, I don't know if it's a version issue
* but my version of Rhino mock doesn't seem to have Is.Matching, So I couldn't introduce test code for delegates.
*/
/* AAA (ARRANGE, ACT, ASSERT)
*
* It maybe more intutive to design tests according to AAA rather than Record and Replay, which is what the above tests were
* using. Instead of setting expectations ahead of actually running our testable code we simply setup our test objects,
* run the code, and then assert that the stuff we expected to happen ... happened. This way of formating tests also requires
* the use of the lambda feature in C# so be wary if you aren't comfortable with functional language principles specifically
* how to use anonymous functions.
*/
[Test]
public void RhinoMock_SetMonths_Proper_Content_Sent_AAA()
{
// Arrange
IWriter Writer = _Mocks.DynamicMock<IWriter>();
_ObjUtilTestable._Writer = Writer;
// Act
_Mocks.ReplayAll();
_ObjUtilTestable.SetMonths("emptyFile.txt");
// Assert
Writer.AssertWasCalled
(writer => writer.ValidateAndWrite
("emptyFile.txt",_ExpectedMonthsAsString));
}
[Test]
public void RhinoMock_GetMonths_Returns_All_Months_AAA()
{
// Arrange
IReader Reader = _Mocks.Stub<IReader>();
_ObjUtilTestable._Reader = Reader;
Reader.Expect(reader => reader.ValidateAndRead("file.txt")).Return(_ExpectedMonthsAsString);
// Act
_Mocks.ReplayAll();
string[] Result = _ObjUtilTestable.GetMonthsStubUsingLocalFactory();
// Assert
Assert.AreEqual(_ExpectedMonths, Result);
}
[TearDown]
public void TearDown()
{
_ObjUtil = null;
_Reader = null;
}
}
}