1

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);
}

1 Answers1

2

I'm learning unit testing by reading "The Art of Unit Testing" and was stuck at mocking event subscription too. But after playing with methods and arguments I found the solution:

[Test]
public void VerifyAttachesToViewEvents()
{
    var viewMock = Mock.Create<IView>();
    Mock.ArrangeSet(() => viewMock.Load += null).IgnoreArguments().OccursOnce();
    var presenter = new Presenter(viewMock);
    Mock.AssertAll(viewMock);
}
v1rusw0rm
  • 117
  • 2
  • 8
  • Oh, after a few minutes of searching for better solution I found exactly the same example at [official Telerik's forums](http://www.telerik.com/community/forums/justmock/general-discussions/test-if-a-presenter-is-subscribed-to-an-event.aspx) – v1rusw0rm Jun 22 '13 at 16:17
  • 1
    Thank you, my Google Fu was weak. – user2506873 Jun 24 '13 at 17:57