Showing posts with label Unit tests. Show all posts
Showing posts with label Unit tests. Show all posts
Monday, 4 May 2009
NUnit 2.5.0 installation problem
I was trying to install NUnit msi package on my computer but the installer told me this: "Please wait while installer finishes determining your disk space requirements.". It hanged forever so I finished intallation. I thought some other running program probably blocks intallation for some reason. I tried to kill some running processes (IE 7.0, Firefox, MS Outlook) and run the installer again with success. I am not sure what exactly happened but it helped at least in my case.
Monday, 20 April 2009
Mocking in C#
During my last assignement I worked on refactoring of our project and part of that was to refactor our test. I find out that we use mocks in there. To be more specific we used Rhino Mocks. I was curious how it works, what are advantages/disadvantages and wanted to learn more.
For better understanding of differences, there are used and a bit modified examples from Martin Fowler's article. The examples are adapted for .NET environment.
For testing there are used following classes:WarehouseImpl class
Order class
Classical (NUnit) test example
This is example how usually NUnit tests are written and as you can see there we do some actions (order.Fill()) and check that results are as expected.
Rhino Mock example
This code shows how to use Rhino.Mocks to test behavior is as expected.
NUnit
Rhino.Mocks
What are mocks (mock objects)
Mock objects are special testing objects which allow developers to test in easier way the behavior of real objects. Classical unit test tests rather the state of objects then their behavior. This is probably the biggest difference between those two test approaches.For better understanding of differences, there are used and a bit modified examples from Martin Fowler's article. The examples are adapted for .NET environment.
For testing there are used following classes:
IWarehouse interface
public interface IWarehouse
{
int GetInvetory(string name);
void Add(string name, int count);
bool HasInventory(string name, int count);
void Remove(string name, int count);
}
WarehouseImpl class
public class WarehouseImpl : IWarehouse
{
private Dictionarystore = new Dictionary ();
public int GetInvetory(string name)
{
if (store.ContainsKey(name))
return store[name];
else
return 0;
}
public void Add(string name, int count)
{
if (store.ContainsKey(name))
store[name] = store[name] + count;
else
store.Add(name, count);
}
public bool HasInventory(string name, int count)
{
if (store.ContainsKey(name))
if (store[name] >= count)
return true;
else
return false;
else
return false;
}
public void Remove(string name, int count)
{
store[name] = store[name] - count;
}
}
Order class
public class Order
{
private string name;
private int count;
private bool isFilled;
public bool IsFilled { get { return isFilled; } }
public Order(string n, int c)
{
name = n;
count = c;
isFilled = false;
}
public void Fill(IWarehouse warehouse)
{
if (warehouse.HasInventory(name, count))
{
warehouse.Remove(name, count);
isFilled = true;
}
}
}
Classical (NUnit) test example
This is example how usually NUnit tests are written and as you can see there we do some actions (order.Fill()) and check that results are as expected.
[TestFixture]
public class NUnitTest
{
private IWarehouse warehouse;
private const string TALISKER = "Talisker";
private const string HIGHLAND_PARK = "Highland Park";
[SetUp]
public void Setup()
{
warehouse = new WarehouseImpl();
warehouse.Add(TALISKER, 50);
warehouse.Add(HIGHLAND_PARK, 25);
}
[Test]
public void TestOrderIsFilledIfEnoughInWarehouse()
{
Order order = new Order(TALISKER, 50);
order.Fill(warehouse);
Assert.IsTrue(order.IsFilled);
Assert.AreEqual(0, warehouse.GetInvetory(TALISKER));
}
[Test]
public void testOrderDoesNotRemoveIfNotEnough()
{
Order order = new Order(TALISKER, 51);
order.Fill(warehouse);
Assert.IsFalse(order.IsFilled);
Assert.AreEqual(50, warehouse.GetInvetory(TALISKER));
}
}
Rhino Mock example
This code shows how to use Rhino.Mocks to test behavior is as expected.
[TestFixture]
public class RhinoTest
{
private const String TALISKER = "Talisker";
[Test]
public void TestFillingRemovesInventoryIfInStock()
{
Order order = new Order(TALISKER, 50);
MockRepository mock = new MockRepository();
IWarehouse warehouseMock = mock.CreateMock();
Expect.Call(warehouseMock.HasInventory(TALISKER, 50)).Return(true).Repeat.Once();
Expect.Call(delegate { warehouseMock.Remove(TALISKER, 50); }).Repeat.Once();
mock.ReplayAll();
order.Fill(warehouseMock);
mock.VerifyAll();
Assert.IsTrue(order.IsFilled);
}
public void TestFillingDoesNotRemoveIfNotEnoughInStock()
{
Order order = new Order(TALISKER, 51);
MockRepository mock = new MockRepository();
IWarehouse warehouseMock = mock.CreateMock();
Expect.Call(warehouseMock.HasInventory(TALISKER, 51)).Return(false).Repeat.Once();
mock.ReplayAll();
order.Fill(warehouseMock);
mock.VerifyAll();
Assert.IsFalse(order.IsFilled);
}
}
Differences between NUnit test and test which uses Rhino.Mocks
As you can see in the examples above, mocking can help you to test easier in cases you do not want your unit tests e.g. access database, send real emails etc. You only test the behavioural of the classes you are insterested in.References
Mocks aren't stubs by Martin FowlerNUnit
Rhino.Mocks
Wednesday, 16 January 2008
How to create unit tests for Windows services in Visual Studio 2005
I needed to write couple of unit tests but it also included to write unit tests for Windows service.
First of all I wanted to create NUnit tests but I do not know how to do that and what should I test. Solution for this was use Visual Studion integrated tests. I added Test project to my solution and after that I used wizard for creating new test classes. Visual Studio created skeletons for testing methods and I only implemented what was necesary to check in the test itself. One thing which is quite cool is that VS generated tests also for private methods. This can be accomplished by using reflection.
Other problem was after I implemented tests and wanted to run them. All tests failed. It was because there was no windows service configuration file. It was necessary to copy app.config from other project to the place where is project test dll (name has to be project_test.dll.config). I set project copy app.config everytime the test project is successfully built (as post build event).
First of all I wanted to create NUnit tests but I do not know how to do that and what should I test. Solution for this was use Visual Studion integrated tests. I added Test project to my solution and after that I used wizard for creating new test classes. Visual Studio created skeletons for testing methods and I only implemented what was necesary to check in the test itself. One thing which is quite cool is that VS generated tests also for private methods. This can be accomplished by using reflection.
Other problem was after I implemented tests and wanted to run them. All tests failed. It was because there was no windows service configuration file. It was necessary to copy app.config from other project to the place where is project test dll (name has to be project_test.dll.config). I set project copy app.config everytime the test project is successfully built (as post build event).
Subscribe to:
Posts (Atom)