Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
Expecting mocks and stubs but getting nulls
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
jamarchist  
View profile  
 More options Apr 15 2009, 2:54 pm
From: jamarchist <jamesryang...@gmail.com>
Date: Wed, 15 Apr 2009 11:54:22 -0700 (PDT)
Local: Wed, Apr 15 2009 2:54 pm
Subject: Expecting mocks and stubs but getting nulls
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?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jamarchist  
View profile  
 More options Apr 15 2009, 3:50 pm
From: jamarchist <jamesryang...@gmail.com>
Date: Wed, 15 Apr 2009 12:50:33 -0700 (PDT)
Local: Wed, Apr 15 2009 3:50 pm
Subject: Re: Expecting mocks and stubs but getting nulls
Relevant:

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

On Apr 15, 1:54 pm, jamarchist <jamesryang...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jamarchist  
View profile  
 More options Apr 15 2009, 4:26 pm
From: jamarchist <jamesryang...@gmail.com>
Date: Wed, 15 Apr 2009 13:26:06 -0700 (PDT)
Local: Wed, Apr 15 2009 4:26 pm
Subject: Re: Expecting mocks and stubs but getting nulls
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?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jamarchist  
View profile  
 More options Apr 15 2009, 5:53 pm
From: jamarchist <jamesryang...@gmail.com>
Date: Wed, 15 Apr 2009 14:53:02 -0700 (PDT)
Local: Wed, Apr 15 2009 5:53 pm
Subject: Re: Expecting mocks and stubs but getting nulls
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim Barcz  
View profile  
 More options Apr 15 2009, 6:28 pm
From: Tim Barcz <timba...@gmail.com>
Date: Wed, 15 Apr 2009 17:28:24 -0500
Local: Wed, Apr 15 2009 6:28 pm
Subject: Re: [RhinoMocks] Re: Expecting mocks and stubs but getting nulls

Glad we can help :-)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »