Expecting mocks and stubs but getting nulls

108 views
Skip to first unread message

jamarchist

unread,
Apr 15, 2009, 2:54:22 PM4/15/09
to Rhino.Mocks
I'm getting some bizarre behavior from Rhino Mocks. It's possible that
my test setup is wrong, and there is a glaring error that I'm staring
right at, but I've been over the code several times and don't see it.
The following test fails with a NullReference exception because
view.ValueDifferenceValidator is null when the view load event is
raised:

[NUNIT.TestFixture]
public class ValueDifferenceValidationPresenterTest
{
private MockRepository mockRepository;
private IValueDifferenceValidationView mockView;
private IServerValidator mockValidator;
private ITextControl from;
private ITextControl to;

private IEventRaiser viewLoad;
private IEventRaiser serverValidate;

[NUNIT.SetUp]
public void CreateTestDoubles()
{
mockRepository = new MockRepository();
mockView =
mockRepository.DynamicMock<IValueDifferenceValidationView>();
mockValidator =
mockRepository.DynamicMock<IServerValidator>();
from = mockRepository.Stub<ITextControl>();
to = mockRepository.Stub<ITextControl>();

Expect.Call(mockView.ValueDifferenceValidator).Return
(mockValidator);
Expect.Call(mockView.FromValueTextBox).Return(from);
Expect.Call(mockView.ToValueTextBox).Return(to);

viewLoad =
Expect.Call(delegate { mockView.Load +=
null; }).IgnoreArguments().GetEventRaiser();
serverValidate =
Expect.Call(delegate { mockValidator.ServerValidate +=
null; }).IgnoreArguments().GetEventRaiser();
}

private void EndArrange()
{
mockRepository.ReplayAll();
}

[NUNIT.Test]
public void Lower_To_Value_Should_Not_Be_Valid()
{
// Arrange
ServerValidateEventArgs validationArguments = new
ServerValidateEventArgs(String.Empty, true);
EndArrange();

// Act
from.Text = "20";
to.Text = "18";

IValueDifferenceValidationPresenter presenterUnderTest =
new ValueDifferenceValidationPresenter(mockView);
viewLoad.Raise(mockView, EventArgs.Empty);
serverValidate.Raise(mockValidator, validationArguments);

// Assert
NUNIT.Assert.IsFalse(validationArguments.IsValid);
}
}

Here is the presenter being tested and view interface:

public interface IValueDifferenceValidationView : IView
{
IServerValidator ValueDifferenceValidator { get; }
ITextControl FromValueTextBox { get; }
ITextControl ToValueTextBox { get; }
}

public class ValueDifferenceValidationPresenter :
IValueDifferenceValidationPresenter
{
private readonly IValueDifferenceValidationView view;

public ValueDifferenceValidationPresenter
(IValueDifferenceValidationView view)
{
this.view = view;

this.view.Load += new EventHandler(view_Load);
}

private void view_Load(object sender, EventArgs e)
{
//this.view.ValueDifferenceValidator.ServerValidate += new
ServerValidateEventHandler(ValueDifferenceValidator_ServerValidate);
this.view.ValueDifferenceValidator.ServerValidate +=new
EventHandler<ServerValidateEventArgs>
(ValueDifferenceValidator_ServerValidate);
}

private void ValueDifferenceValidator_ServerValidate(object
source, ServerValidateEventArgs args)
{
// Some logic
}
}

I expect the test to pass because the following works perfectly:

[NUNIT.TestFixture]
public class SomeOtherValidationPresenterTest
{
private MockRepository mockRepository;
private ISomeOtherValidationView mockView;
private IServerValidator mockValidator;
private IEventRaiser viewLoad;
private IEventRaiser serverValidate;

[NUNIT.SetUp]
public void InitializeMocks()
{
mockRepository = new MockRepository();
mockView =
mockRepository.DynamicMock<ISomeOtherValidationView>();
mockValidator =
mockRepository.DynamicMock<IServerValidator>();

Expect.Call(mockView.ValueValidator).Return
(mockValidator);
viewLoad =
Expect.Call(delegate { mockView.Load +=
null; }).IgnoreArguments().GetEventRaiser();
serverValidate =
Expect.Call(delegate { mockValidator.ServerValidate +=
null; }).IgnoreArguments().GetEventRaiser();
}

public void EndArrange()
{
mockRepository.ReplayAll();
}

[NUNIT.Test]
public void Empty_Value_Should_Be_Valid()
{
// Arrange
ServerValidateEventArgs validationArguments = new
ServerValidateEventArgs(String.Empty, false);
EndArrange();

// Act
ISomeOtherValidationPresenter presenterUnderTest = new
SomeOtherValidationPresenter(mockView);
viewLoad.Raise(mockView, EventArgs.Empty);
serverValidate.Raise(mockValidator, validationArguments);

//Assert
NUNIT.Assert.IsTrue(validationArguments.IsValid);
}
}

public class SomeOtherValidationPresenter :
ISomeOtherValidationPresenter
{
private readonly ISomeOtherValidationView view;

public SomeOtherValidationPresenter(ISomeOtherValidationView
view)
{
this.view = view;

this.view.Load += new EventHandler(view_Load);
}

private void view_Load(object sender, EventArgs e)
{
this.view.ValueValidator.ServerValidate +=new
EventHandler<ServerValidateEventArgs>(ValueValidator_ServerValidate);
}

private void ValueValidator_ServerValidate(object source,
ServerValidateEventArgs args)
{
// Some logic
}
}

public interface ISomeOtherValidationView : IView
{
IServerValidator ValueValidator { get; }
}

When I debug in Visual Studio I actually see some more strange
behavior... It looks like on the initial valuation of the
ValueDifferenceValidator, FromValueTextBox, and ToValueTextBox
properties (that is, the evaluation done at first by the debugger),
the mockView **IS** returning the mocks and stubs I have set up.
Further evaluation returns null for these properties. I suspect there
is something I'm doing wrong with the record/replay model, but both
scenarios are almost **exactly** the same.

Can anyone explain this? What obvious thing am I missing?

jamarchist

unread,
Apr 15, 2009, 3:50:33 PM4/15/09
to Rhino.Mocks
Relevant:

I'm using Rhino Mocks 3.5 for .NET 2.0 (and some old ass version of
NUnit)

jamarchist

unread,
Apr 15, 2009, 4:26:06 PM4/15/09
to Rhino.Mocks
So, I did the obvious thing here and commented out my use of the
Stubs, which was the only difference between the tests, and this
solved the problem of getting null back for the validator property
(the mockValidator was returned as expected when called by the
presenter). I'm not a pro Rhino Mocks user, but I wouldn't consider
myself a novice either. I'm making some amateur (no pun intended)
mistake. What is wrong with my expectations of how this should work?

jamarchist

unread,
Apr 15, 2009, 5:53:02 PM4/15/09
to Rhino.Mocks
Sorry, I solved this. More context would have been helpful for anyone
who tried to run these tests (the logic in the servervalidate event
handlers). The problem was that I wasn't using <b>.Repeat.Any()</b> on
the return expectation for the different properties.

Thanks for anyone who looked at this and thought about it. My
apologies for taking your time... it was a stupid mistake as I
suspected.

-Ryan

Tim Barcz

unread,
Apr 15, 2009, 6:28:24 PM4/15/09
to Rhino...@googlegroups.com
Glad we can help :-)
Reply all
Reply to author
Forward
0 new messages