12 July, 2011

Second test with MOQ

Continuing with the testing of MOQ, I created a second test.


This time, it should access a set of documents and retrieve those belonging to an user.
For that, the new classes User, Project and DocumentList are added to Models, along with their interfaces IUser, IProject and IDocumentList respectively.
The Document class will be modified to contain a reference of the Project it belongs to and the User who is its owner.


All those objects will be mocked and the test will access the document list to retrieve those belonging to a user specified.


The test looks like this:


[TestMethod]
public void RetrieveAllDocumentsFromUser()
{
var mUser1 = new Mock<IUser>();
var mUser2 = new Mock<IUser>();
var mDocumentList = new Mock<IDocumentList>();
var mDocument1 = new Mock<IDocument>();
var mDocument2 = new Mock<IDocument>();
var mDocument3 = new Mock<IDocument>();

mUser1.Setup(x => x.ID).Returns(1);
mUser1.Setup(x => x.name).Returns("Test User 01");
mUser1.Setup(x => x.password).Returns("");
mUser1.Setup(x => x.screenName).Returns("User01");

mUser2.Setup(x => x.ID).Returns(2);
mUser2.Setup(x => x.name).Returns("Test User 02");
mUser2.Setup(x => x.password).Returns("");
mUser2.Setup(x => x.screenName).Returns("User02");

mDocument1.Setup(x => x.ID).Returns(1);
mDocument1.Setup(x => x.name).Returns("Test Document 01");
mDocument1.Setup(x => x.owner).Returns(mUser1.Object);

mDocument2.Setup(x => x.ID).Returns(2);
mDocument2.Setup(x => x.name).Returns("Test Document 02");
mDocument2.Setup(x => x.owner).Returns(mUser2.Object);

mDocument3.Setup(x => x.ID).Returns(3);
mDocument3.Setup(x => x.name).Returns("Test Document 03");
mDocument3.Setup(x => x.owner).Returns(mUser1.Object);

mDocumentList.Setup(x => x.documentList).Returns(new List
());

mDocumentList.Object.addDocumentToList(mDocument1.Object);
mDocumentList.Object.addDocumentToList(mDocument2.Object);
mDocumentList.Object.addDocumentToList(mDocument3.Object);
Assert.IsNotNull(mDocumentList.Object.documentsOfUser(1));
Assert.AreEqual(2, mDocumentList.Object.documentsOfUser(1).Count); 
}



In the last two lines, the mock object for the document list asserts that the list of documents returned exists and that it contains exactly 2 elements.


At a first run, the test didn't pass.
The method of retrieving the documents in the list was checked and changed, but the test still didn't pass.
Further investigation and debugging revealed an error in the way the items are inserted in the list.


Because of that, a new test case will be created to try the creation of the document list and the insertion of documents in it.

10 July, 2011

A View on MOQ

MOQ is a Mock Object Framework, used to build mock objects for the testing of a project.
As the software development process that will be followed is TDD, the building of tests will be a central part, and mock objects are very interesting in this field.

After installing MOQ, I will write a simple test to see how it works.

The test will consist in retrieving a Document object (like in a search) and asserting that it exists. For that, a Document object and an IDocument interface are created in the Models folder of the MEDManagerMain project. My purpose is to mock Document in MEDManagerMain.Tests.

Once written, the test looks like this:

        [TestMethod]
        public void RetrieveDocumentFromID()
        {
            var mDocument = new Mock<IDocument>();
            mDocument.Setup(x => x.ID).Returns(1);
            mDocument.Setup(x => x.name).Returns("Test Document");

            Assert.AreEqual("Test Document", mDocument.Object.name);
            Assert.AreEqual(1, mDocument.Object.ID);
        }

Test is run and it passes.

Sources:


An Introduction

MEDManager stands for Multi Electronic Document Manager, and intends to be a software for the management of documents in the context of a final project of a degree in the University.


The development of MEDManager will follow the Test-Driven Development (TDD) software development process and will rely on Agile software development techniques.


This journal will encompass notes on the process of development, ideas that may be implemented, views on technologies that may be used and advances made.