Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 295235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:26:23+00:00 2026-05-12T06:26:23+00:00

I’m setting up some RhinoMock tests but I can’t work out why my expectations

  • 0

I’m setting up some RhinoMock tests but I can’t work out why my expectations are failing.

Here are the class/ interface I’m testing:

public class LogOn {
    public virtual ILogOn View { get; set; }
    public virtual IDataProvider DataProvider { get; set; }

    public void SetUp(ILogOn view) {
        this.View = view;
        this.DataProvider = ... //using dependancy injection to do the data provider, so I want it faked in tests
    }
    public void SetUpEvents() {

        this.View.Submit += new EventHandler(View_Submit);
    }

    void View_Submit(object sender, EventArgs e) {
        if ( this.DataProvider.LogOn(this.Username) ) {
            this.View.SubmitSuccess();
        } else {
            this.View.SubmitFailure("Username is incorrect");
        }
    }
}

public interface ILogOn {
    string Username { get; set; }
    event EventHandler Submit;
    void SubmitSuccess();
    void SubmitFailure(string message);
}

And here is my test method:

[TestMethod]
public void LogOnFailure() {
    var dataProvider = MockRepository.CreateStub<DataProvider>();
    var presenter = MockRepository.CreateMock<LogOn>();
    var view = MockRepository.CreateMock<ILogOn>();

    dataProvider.Expect(d => d.LogOn(null)).Return(true).Repeat.Any();

    presenter.Expect(p => p.DataProvider).Return(dataProvider).Repeat.Any();
    presenter.Expect(p => p.View).Return(view).Repeat.Any();
    presenter.Expect(p => p.SetUpEvents()).CallOriginalMethod();

    view.Expect(v => v.Username).Return("invalid").Repeat.Any();
    view.Expect(v => v.SubmitFail(null)).Constraints(Is.Same("Username is incorrect"));

    presenter.SetUp(view);
    presenter.SetUpEvents();

    view.Raise(v => v.Submit += null, null, EventArgs.Empty);

    presenter.VerifyAllExpectations();
    view.VerifyAllExpectations();
}

The expectation that is failing is:

view.Expect(v => v.SubmitFail(null)).Constraints(Is.Same("Username is incorrect"));

(indicated by view.VerifyAllExpectations)

It says that that method is never executed, but when using the debugger I can step through and LogOn.View is accessed, does call the SubmitFailure method (with that argument) and return correctly.

I can’t work out what is missing as watching the code does indicate that everything is executed at the right time and with the right values.

Edit: Ok, so I let out the code which is why I was mocking the LogOn class, it has a dependancy of an external data provider (which I’m stubbing as I don’t care how it works). My appologies, I thought I was making this clearer but just made is worse!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T06:26:23+00:00Added an answer on May 12, 2026 at 6:26 am

    The LogOn class is your system under test, so you should not be mocking that. You want to test that the LogOn class behaves as it should in the case of an invalid username. You are able to determine the correct behavior by passing in a mocked view that sets up the scenario you want. Try changing your test to what I have below.

    [TestMethod]
    public void LogonFailure()
    {
        var presenter = new LogOn();
        var view = MockRepository.CreateMock<ILogOn>();
    
        view.Expect(v => v.Username).Return("invalid").Repeat.Any();
        view.Expect(v => v.SubmitFail(null)).Constraints(Is.Same("Username is incorrect"));
    
        presenter.Setup(view);
    
        view.Raise(v => v.Submit += null, null, EventArgs.Empty);
    
        view.VerifyAllExpectations();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.