Moq - Verify no methods were called except one - c#

I have a controller with a method that reads configuration to determine which other method(s) to call. Depending on the config, it may call zero, one, or all of the WorkerMethodN() methods.
public class MyController
{
public virtual bool EntranceMethod()
{
// read configuration to determine which methods to call
}
public virtual void WorkerMethod1() { ... }
public virtual void WorkerMethod2() { ... }
public virtual void WorkerMethod3() { ... }
}
I am trying to test this EntranceMethod() and my first test is to determine behavior when the configuration is empty. When the configuration returns nothing, I want to ensure that none of the WorkerMethodN() methods are called.
My test so far:
[TestMethod]
public void ShouldNotCallAnyMethodsWhenConfigurationReturnsNull()
{
this.mockConfigurationReader
.Setup(cr => cr.GetEnabledConfigurations())
.Returns((IEnumerable<Configuration>)null);
Mock<MyController> mockController =
new Mock<MyController>(MockBehavior.Strict, this.mockConfigurationReader.Object);
mockController.Object.EntranceMethod();
// todo: verify no additional methods are called
}
This call fails with the Exception: invocation failed with mock behavior Strict. All invocations on the mock must have a corresponding setup. when the call to EntranceMethod() is made.
How do I use MockBehavior.Strict and setup my controller to call EntranceMethod() and verify that no other methods are called? If I call .Setup() on my EntranceMethod(), it won't run the actual code I want it to. But if I don't call .Setup(), I get an exception.

For demonstrative purposes only, assume the following
public class Configuration {
}
public interface IConfigurationReader {
IEnumerable<Configuration> GetEnabledConfigurations();
}
public class MyController {
private IConfigurationReader configReader;
public MyController(IConfigurationReader configReader) {
this.configReader = configReader;
}
public virtual bool EntranceMethod() {
// read configuration to determine which methods to call
var config = configReader.GetEnabledConfigurations();
//...code for example purposes only
if (config != null) {
WorkerMethod1();
WorkerMethod2();
WorkerMethod3();
return true;
}
return false;
}
public virtual void WorkerMethod1() {
//...
}
public virtual void WorkerMethod2() {
//...
}
public virtual void WorkerMethod3() {
//...
}
}
Remove MockBehavior.Strict, enable CallBase = true then setup and check the other methods were not called using .Verify(......., Times.Never())
[TestClass]
public class MyControllerTest {
[TestMethod]
public void ShouldNotCallAnyMethodsWhenConfigurationReturnsNull() {
//Arrange
var mockConfigurationReader = new Mock<IConfigurationReader>();
mockConfigurationReader
.Setup(cr => cr.GetEnabledConfigurations())
.Returns((IEnumerable<Configuration>)null);
var mockController = new Mock<MyController>(mockConfigurationReader.Object) {
CallBase = true
};
//Act
mockController.Object.EntranceMethod();
//Assert
// todo: verify no additional methods are called
mockController.Verify(_ => _.WorkerMethod1(), Times.Never());
mockController.Verify(_ => _.WorkerMethod2(), Times.Never());
mockController.Verify(_ => _.WorkerMethod3(), Times.Never());
}
}
Reference Moq Quickstart

Modern answer (Moq 4.8 or later):
mock.Verify(...);
mock.VerifyNoOtherCalls();
That code does 2 things:
Verifies that any expected calls were made
Verifies that no other calls were made
Source: Moq Quickstart

Related

How can I determine if a function wrapped in an if statement gets called?

I have a class, like this one:
public class SomeClass: ISomeClass
{
public async Task<ServiceResponse> PerformAction(Context context)
{
if(IsPartner(context))
{
SendTextMessageAsync(context);
}
}
public bool IsPartner(Context context)
{
return SomeStaticHelperClass.GetIsPartner(context.PartnerId);
}
public async void SendTextMessageAsync(Context context)
{
// sends the message
}
}
ISomeClass exposes all three methods.
I'm using Moq and I would like to write a test that verifies
when IsPartner returns true
then SendTextMessage is called once.
I've tried everything I can think of.
This is my latest attempt:
public class ReceiptServiceTests
{
[Fact]
public async void SendReceiptsProofsNeededNudgeAsync_ShouldCallSendTextMessageAsync_WhenSessionPartnerIdIsNotNullOrEmptyString()
{
var context = new Context { PartnerId = "12345" };
var mockSomeClass = new Mock<ISomeClass>();
// force IsPartner to return true
mockSomeClass.Setup(m => m.IsPartner(context)).Returns(true);
// call PerformAction which should call SendTextMessageAsync
// because IsPartner will return true
await mockSomeClass.Object.PerformAction(context);
// verify SendTextMessageAsync was actually called
mockSomeClass.Verify(m => m.SendTextMessageAsync(It.IsAny<Context>()), Times.AtLeastOnce);
}
}
I keep getting following error:
(Expected invocation on the mock at least once, but was never performed: m => m.SendTextMessageAsync)
which i'm guessing is because SendTextMessageAsync is never called. I'm stuck here though, I don't understand how to properly call it.
I know some (or a lot) of this code doesn't make sense, I've changed it for brevity.
What I'm really just trying to figure out is how can I determine if a function (SendTextMessageAsync) is called when it is wrapped in an if statement that calls another function that returns true.
As rgvlee as already pointed out you can only mock dependencies and verify their method calls.
In your case SomeStaticHelperClass is your dependency and its GetIsPartner method that could be verified.
Unfortunately static classes are not good candidates for mocking. If you could replace that static class reference to an interface then you would be able to mock that easily.
SomeClass uses IPartnerService
public class SomeClass: ISomeClass
{
private readonly IPartnerService partnerSvc;
public SomeClass(IPartnerService partnerSvc)
{
this.partnerSvc = partnerSvc;
}
public async Task<ServiceResponse> PerformAction(Context context)
{
if(IsPartner(context))
{
SendTextMessageAsync(context);
}
}
public bool IsPartner(Context context)
{
return partnerSvc.GetIsPartner(context.PartnerId);
}
public async void SendTextMessageAsync(Context context)
{
// sends the message
}
}
SomeClass is using a IPartnerService mock during unit testing
public class ReceiptServiceTests
{
[Fact]
public async Task GivenAPartner_WhenICallPerformAction_ThenItCallsPartnerService()
{
//Arrange
const string partnerId = "12345";
var context = new Context { PartnerId = partnerId };
var partnerSvcMock = new Mock<IPartnerService>();
partnerSvcMock
.Setup(svc => svc.GetIsPartner(partnerId))
.Returns(true);
var SUT = new SomeClass(partnerSvcMock);
//Act
_ = await SUT.PerformAction(context);
//Assert
partnerSvcMock.Verify(svc => svc.GetIsPartner(partnerId), Times.Once);
}
}

Match a func delegate in argument matcher in nsubstitute Received method

I am trying to check if a method is called a specific number of times on a mock instance of a class. The problem is the method has a func delegate and that is not matching.
I have the following scenario:
public interface ISomeService: IService
{
Task CleanupMethod(CancellationToken cancellationToken);
}
public interface I
{
Task invokedMethod(string aName, Func<IService, Task> action);
}
public class ClassGoingToBeUnitTested
{
// instance of I
private I instanceOfI;
// a list of names.
private static readonly string[] serviceNames =
{
"Name1",
"Name2"
};
// constructor
public ClassGoingToBeUnitTested(I passedInstance)
{
this.instanceOfI = passedInstance;
}
public void methodToBeUnitTested(object cancellationToken)
{
// my logic here
// here I am calling invokedMethod method to known number of times.
// something like this.
try
{
IEnumerable<Task> someTasks = serviceNames.Select(
name => this.instanceOfI.invokedMethod(
name,
service => ((ISomeService)service).CleanupMethod((CancellationToken)cancellationToken)
));
// here I run the tasks
Task.WaitAll(someTasks.ToArray());
}
catch
{
// proper catching of exceptions
}
// other logic
}
}
[TestClass]
public class ClassGoingToBeUnitTestedTest
{
// mock of I interface
private I IMock;
// ClassGoingToBeUnitTested object
ClassGoingToBeUnitTested classGoingToBeUnitTested;
[TestInitialize]
public void init()
{
this.IMock = Substitute.For<I>();
this.classGoingToBeUnitTested = new ClassGoingToBeUnitTested(this.IMock);
}
[TestMethod]
public void methodToBeUnitTested_Success()
{
// Arrange
var cancellationTokenSource = new CancellationTokenSource();
// Act
this.classGoingToBeUnitTested.methodToBeUnitTested(cancellationTokenSource.Token);
// Assert
// this is throwing exception.
this.IMock.Received(1).invokedMethod(
"Name1",
service => ((ISomeService)service).CleanupMethod((CancellationToken)cancellationTokenSource.Token)); // problem lies in this line.
}
}
In the above code if I change ((ISomeService)service).CleanupMethod((CancellationToken)cancellationTokenSource.Token)) to Arg.Any<Func<IService, Task>(), it runs perfectly. But I don't want to check that for my use case.
Till now, I have been able to debug that the argument matcher is matching the delegates by reference and hence is not able to match the arguments correctly. But I am not able to correctly match the arguments.
I also tried to invoke the delegate but I did not succeed. I think I am missing something. Any help would be highly appreciated.
I solved this by using Invoke. I first mocked the behaviour of invokedMethod to invoke a mocked serviceInstanceMock whenever it is called and then checked the number of times CleanupMethod is called on the serviceInstanceMock itself.
[TestClass]
public class ClassGoingToBeUnitTestedTest
{
// mock of I interface
private I IMock;
// mock of ISomeService
private ISomeService someServiceMockInstance;
// ClassGoingToBeUnitTested object
ClassGoingToBeUnitTested classGoingToBeUnitTested;
[TestInitialize]
public void init()
{
this.IMock = Substitute.For<I>();
this.ISomeService = Substitute.For<ISomeService>();
this.classGoingToBeUnitTested = new ClassGoingToBeUnitTested(this.IMock);
}
[TestMethod]
public void methodToBeUnitTested_Success()
{
// Arrange
var cancellationTokenSource = new CancellationTokenSource();
this.IMock.invokedMethod(
Arg.Any<string>,
Arg.Do<Func<IService, Task>(x => x.Invoke(this.someServiceMockInstance)));
// Act
this.classGoingToBeUnitTested.methodToBeUnitTested(cancellationTokenSource.Token);
// Assert
this.IMock.Received(1).invokedMethod(
"Name1",
Arg.Any<Func<IService, Task>()); // changed this
this.IMock.Received(1).invokedMethod(
"Name2",
Arg.Any<Func<IService, Task>()); // added this as well
// adding to check the Received call on the service instance
this.someServiceMockInstance.Received(2).CleanupMethod(cancellationTokenSource.Token);
}
}

Mocking an object initialised inside a method under test

I am trying to test a class which instatiates another class within it. It is the instatinated class call I want to mock.
Is this possible or is they a way around it/ simpler way I am missing?
Below I have written up a simpler example which still has the same problemts as my main code.
public interface my_interface
{
int returns_25();
}
public class class_i_want_to_mock : my_interface
{
public int returns_25()
{
// TEST SHOULD FAIL SO NEED MOCK TO PASS THE CASE
return 7645745;
}
}
In another namespace (it has access):
public class class_to_test
{
public static int returns_25()
{
class_i_want_to_mock _tempClass= new class_i_want_to_mock ();
// Will return 7645745 unless moq changes return value
int _temp_int = _tempClass.returns_25()
return _temp_int;
}
}
My test (which fails):
[Test]
public void test_returns_25()
{
// Mock
Mock<my_interface> myMock = new Mock<my_interface>();
myMock.Setup(m => m.returns_25()).Returns(25);
// Act
int return_number = class_to_test.returns_25();
// Assert
Assert.AreEqual(25, return_number);
}
Here is working code thanks to the injection idea from JSteward above.
Code is in same order
public interface my_interface
{
int returns_25();
}
public class class_i_want_to_mock : my_interface
{
public int returns_25()
{
// TEST SHOULD FAIL SO NEED MOCK TO PASS THE CASE
return 7645745;
}
}
Next class, notice the injection of the interface
public class class_to_test
{
public static int returns_25(my_interface _temp_interface)
{
// Will return 7645745 unless moq changes return value
int _temp_int = _temp_interface.returns_25()
return _temp_int;
}
}
And the test. Note the object of the Mock class has to be used (myMock.Object)
[Test]
public void test_returns_25()
{
// Mock
Mock<my_interface> myMock = new Mock<my_interface>();
myMock.Setup(m => m.returns_25()).Returns(25);
// Act
int return_number = class_to_test.returns_25(myMock.Object);
// Assert
Assert.AreEqual(25, return_number);
}

Nunit: Testing legacy code (private void) method

I have been tasked with building unit tests for a bunch of legacy code. The specific task/goal for the below method is to test that the messageProcessor.ProcessCustomerPhoneContactInfo(currentPhoneContact) method is being called. I am also posting the test stub I have written so far but I would appreciate some direction because I think I am going down a rabbit hole here. How can I fill in the blanks on my test?
Method being tested:
private void logPhoneCallDialog_SaveContact(Contact currentPhoneContact)
{
if (currentPhoneContact != null)
{
RefreshRenewalActivity();
if (currentPhoneContact.TypeId == ResultType.TookAppointment)
}
NotifyServerOfActivity();
ApplyAppointmentFilters();
this.Activate();
var messageProcessor = new MessageProcessor();
messageProcessor.ProcessCustomerPhoneContactInfo(currentPhoneContact);
}
Test:
[TestFixture, RequiresSTA]
class BucketBrowserTest
{
[Test]
public void logPhoneCallDialog_SaveContact()
{
//Arrange
//Act
//Assert
}
}
Method that calls above method
private void ShowPhoneCallLoggerDialog()
{
PhoneCallLoggerDialog dialog = new PhoneCallLoggerDialog(CurrentCustomer, CurrentBucket.BucketTypeId);
dialog.Owner = this;
dialog.SaveContact += new PhoneCallLoggerDialog.SaveContactHandler(logPhoneCallDialog_SaveContact);
dialog.ShowDialog();
}
Event Handler for calling method
public delegate void SaveContactHandler(PhoneContact currentPhoneContact);
public event SaveContactHandler SaveContact;
Based on the additional information you've supplied, I'm going to outline my assumptions before describing a possible solution:
You're able to safely construct an instance of this class, without calling anything out of process
Calling logPhoneCallDialog_SaveContact(), won't trigger side effects that prevent it from being tested
When refactoring legacy code, you often have to make design choices that you would normally avoid. This can include:
Testing implementation details
Making methods public or internal
Adding light abstractions that simply facilitate testing
In order to get a test around this, you're going to have to do at least one of those things.
Firstly, make logPhoneCallDialog_SaveContact public:
public void logPhoneCallDialog_SaveContact(Contact currentPhoneContact)
{
// same body as before
}
Next, extract a method that holds the entire body of the first one, to end up with this:
public void logPhoneCallDialog_SaveContact(Contact currentPhoneContact)
{
SaveContact(currentPhoneContact);
}
private void SaveContact(Contact currentPhoneContact)
{
if (currentPhoneContact != null)
{
RefreshRenewalActivity();
// This code from your example doesn't compile.
if (currentPhoneContact.TypeId == ResultType.TookAppointment)
}
NotifyServerOfActivity();
ApplyAppointmentFilters();
this.Activate();
var messageProcessor = new MessageProcessor();
messageProcessor.ProcessCustomerPhoneContactInfo(currentPhoneContact);
}
Make the new method public:
public void SaveContact(Contact currentPhoneContact)
{
// same body as before
}
If you haven't already, extract an interface for MessageProcessor:
public interface IMessageProcessor
{
ProcessCustomerPhoneContactInfo(Contact currentPhoneContact);
}
public class MessageProcessor : IMessageProcessor
{
public void ProcessCustomerPhoneContactInfo(Contact currentPhoneContact)
{
// implementation
}
}
Now modify the methods like so:
public void logPhoneCallDialog_SaveContact(Contact currentPhoneContact)
{
var messageProcessor = new MessageProcessor();
SaveContact(currentPhoneContact, messageProcessor);
}
public void SaveContact(
Contact currentPhoneContact,
IMessageProcessor messageProcessor)
{
if (currentPhoneContact != null)
{
RefreshRenewalActivity();
if (currentPhoneContact.TypeId == ResultType.TookAppointment)
}
NotifyServerOfActivity();
ApplyAppointmentFilters();
this.Activate();
messageProcessor.ProcessCustomerPhoneContactInfo(currentPhoneContact);
}
Now write your unit tests against SaveContact, mocking IMessageProcessor, instead of against logPhoneCallDialog_SaveContact.
Edit
Here's an example, as requested. It's been a while since I've used Moq - which was in your original question - so the syntax may not be quite right, but something like this:
[Test]
public void SavesContact()
{
// Arrange
var contact = new Contact();
var messageProcessor = new Mock<IMessageProcessor>();
var subject = // whatever class contains the logPhoneCallDialog_SaveContact method
// Act
subject.SaveContact(contact, messageProcessor.Object);
// Assert
messageProcessor.Verify(x => x.ProcessCustomerPhoneContactInfo(contact), Times.Once());
}
Also test the case where contact is null.
With the code as it stands, you cannot mock out the messageProcessor, but with a few changes, you could:
IMessageProcessorFactory _messageProcessorFactory;
public TheConstructor(IMessageProcessorFactory processorFactory)
{
_messageProcessorFactory = processorFactory;
}
private void logPhoneCallDialog_SaveContact(Contact currentPhoneContact)
{
if (currentPhoneContact != null)
{
RefreshRenewalActivity();
if (currentPhoneContact.TypeId == ResultType.TookAppointment)
}
NotifyServerOfActivity();
ApplyAppointmentFilters();
this.Activate();
var messageProcessor = _messageProcessorFactory.Create();
messageProcessor.ProcessCustomerPhoneContactInfo(currentPhoneContact);
}
Then you can Moq/Mock the interface and find out if the function was called.

Limiting Scope of UnitTest using Rhino stubs

I'm working on a unit test for a service method, that has dependencies. Simplified:
public class ConditionChecker
{
private SqlConnection _connection;
public bool CanDoSomething()
{
return _connection.State == ConnectionState.Open;
}
}
public class A
{
public ConditionChecker Checker { get; set; }
public bool CanInvokeA()
{
return Checker.CanDoSomething();
}
}
[TestClass]
public class ATests
{
[TestMethod]
public void TestCanInvokeA()
{
// arrange
A a = new A();
ConditionChecker checker = MockRepository.GenerateStub<ConditionChecker>();
checker.Stub(x => x.CanDoSomething()).Return(true);
a.Checker = checker;
// act
bool actual = a.CanInvokeA();
// assert
Assert.AreEqual(true, actual);
}
}
What I want is to completely bypass the implementation of ConditionChecker.CanDoSomething, which is why I stub the call, still I run into a null reference Exception during my test, since the _connection member is not set. What am I doing wrong here?
You just mark your method as virtual, it will work:
public virtual bool CanDoSomething()
{
}
Since behind the scene Rhino Mock will create a dynamic proxy for ConditionChecker, so you need to mark virtual to allow Rhino Mock to override it.

Categories

Resources