I'm new to TDD and unit testing and I have JustMock commercial version and a week to learn how to use it. Below are an interface, a class and a test method which passes. I had to add the Class1 faceDoesSomething to get it to compile and I'd like to also do an arrange and assert that it does set action to "smile." Is there a way to that?
Edit: I'm using an example that doesn't use JustMock to create a solution using JustMock. I realized that example tests that the delegate does in fact subscribe to the event so I need to do that before I worry about what it returns. I've modified the code below to be closer to example I'm working from by adding a test method ClassRegistersAsListenerForDoesSomethingEvent. I believe I'm missing an assert but I'm not sure. I do know the test fails so I'm doing something right.
At some point in this process do I actually start using tests on the real classes and not just on the Mocks?
public delegate void IFace1EventDelegate(object sender, IFace1 face);
public interface IFace1
{
string Action { get; set; }
int DoesSomethingListenerCount { get; }
event IFace1EventDelegate DoesSomething;
}
public class Class1
{
public event IFace1EventDelegate DoesSomething;
IFace1 face;
private string action = string.Empty;
public Class1(IFace1 Face)
{
face = Face;
face.DoesSomething += new IFace1EventDelegate(faceDoesSomething);
}
public string Action
{
get { return action; }
set { action = value; }
}
public int DoesSomethingListenerCount
{
get
{
if (DoesSomething != null)
{
return DoesSomething.GetInvocationList().Count();
}
return 0;
}
}
private void faceDoesSomething(object sender, IFace1 face)
{
}
}
[TestMethod]
public void FaceShouldSmile()
{
// arrange
var Smiley = Mock.Create<IFace1>();
var Classy = Mock.Create<Class1>(Smiley);
Mock.Arrange(() => Classy.Action).Returns("Frown");
// act
string s = Classy.Action;
// assert
Assert.AreEqual("Frown", s);
}
[TestMethod]
public void ClassRegistersAsListenerForDoesSomethingEvent()
{
// arrange
var Smiley = Mock.Create<IFace1>();
var Classy = Mock.Create<Class1>(Smiley);
// act
int listenersCountAfter = 1;
int actualListenersCount = Classy.DoesSomethingListenerCount;
// assert
Assert.AreEqual(listenersCountAfter, actualListenersCount);
}