1

I'm purposefully trying to pass a bad setup for moq and I'm expecting an error. I'm passing a class to my setup method, where I would like the instance variables under a certain criteria. Since I create a new instance of the class, I would expect an error, since all the instance variables are null. However, nothing gets thrown?

        var mockParams = new object[] { mockRequestRepo.Object, mockNotificationSvc.Object, mockLogger.Object, mockNotificationBuilder.Object };
        var mockActivityReportBO = new Mock<ActivityReport>(mockParams);
        // Instance variables for class.
        mockActivityReportBO.Setup(x => x.AddReport(It.Is<ActivityReport>(
            x => x.Title == It.IsAny<string>()
            && x.Limits == It.IsAny<string>()
            && x.Description == It.IsAny<string>()
            && x.DueDate == It.IsInRange(DateTime.Now.AddDays(12), DateTime.MaxValue, Range.Inclusive)
            && x.CountyNumber == It.IsInRange(1, 5, Range.Inclusive)
            && x.ActivityReportID == It.IsInRange(1, 12, Range.Inclusive)
        )));
        var report = new ActivityReport();
        // No error thrown
        mockActivityReportBO.Object.AddReport(report);
2
  • 1
    First, by design, it wont throw an exception unless the mock's behavior is strict. Second, the setup is not being done correctly by creating the argument matcher outside of the setup expression. How do you initialize the mock? Commented Feb 9, 2020 at 23:50
  • Without a minimal reproducible example that clarifies your specific problem or additional details to highlight exactly what was done, it will be difficult to reproduce the problem that would allow a better understanding of what is the actual problem. Commented Feb 9, 2020 at 23:52

1 Answer 1

2

First, by design, it wont throw an exception unless the mock's behavior is strict.

Reference Moq Quick start: Customizing Mock Behavior

Make mock behave like a "true Mock", raising exceptions for anything that doesn't have a corresponding expectation: in Moq slang a "Strict" mock; default behavior is "Loose" mock, which never throws and returns default values or empty arrays, enumerables, etc. if no expectation is set for a member

note: emphasis mine

var mock = new Mock<IFoo>(MockBehavior.Strict);

Second, the Setup is not being done correctly by creating the argument matcher outside of the Setup expression

mockActivityReportBO.Setup(_ => _.AddReport(It.Is<ActivityReport>(
        x => x.Title == It.IsAny<string>()
        && x.Limits == It.IsAny<string>()
        && x.Description == It.IsAny<string>()
        && x.DueDate == It.IsInRange(DateTime.Now.AddDays(12), DateTime.MaxValue, Range.Inclusive)
        && x.CountyNumber == It.IsInRange(1, 5, Range.Inclusive)
        && x.ActivityReportID == It.IsInRange(1, 12, Range.Inclusive)
    )
));

Reference Moq Quick start: Matching Arguments

Update

I would like the mock exception thrown when I pass a parameter, that contains data outside the setup. For example, If I try to call add report, and I pass a report object where the member variable Title = null, I would like an exception thrown.

Be explicit about what to expect and tell the setup to throw an exception when that happens

For example

mockActivityReportBO
    .Setup(x => x.AddReport(It.Is<ActivityReport>(y => y.Title == null)))
    .Throws<InvalidOperationException>(); //<-- replace with desired Exception

The above will throw an exception when you pass a report object where the member variable Title = null into the mock. (Using default behavior mode "Loose" of course. Not "Strict")

Sign up to request clarification or add additional context in comments.

6 Comments

I've made your suggested changes. However, now I get a Moq.MockException saying All invocations on the mock must have a corresponding setup. Do I have to setup all my methods? I would just like to mock this one.
@usr4896260 Strict requires all members be setup, not just one.
Ok, that being said, since I just want this one method setup, is there another way around this?
@usr4896260 You need to explain what it is you actually want to do. Why do you want an exception thrown?
I would like the mock exception thrown when I pass a parameter, that contains data outside the setup. For example, If I try to call add report, and I pass a report object where the member variable Title = null, I would like an exception thrown.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.