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.
Related
I have a class that I am trying to test out through writing some unit test in C#. I am also using Moq library in my unit test for mocking.
Below are some of the methods in my class which I want to mock so that they can return some dummy data which I want to return always.
public class DataManager
{
public DataManager(ILoggingService loggingService, IConfiguration configuration)
{
// ...
}
// how to mock this to return dummy data?
private DataHolder GetData()
{
//... return DataHolder object
}
// how to mock this to return dummy data?
private IEnumerable<ProcessClient> GetClientValues(string value)
{
//... return IEnumerable<ProcessClient> object
}
}
This is how I am testing it out:
private Mock<ClientUtility> utility;
[SetUp]
public void Setup()
{
utility = new Mock<ClientUtility>();
}
[Test]
public void Test1()
{
// what should I do here so that "GetData" and "GetClientValues" return dummy data when I call below class constructor?
var testName = "config1";
var testContent = "{}";
utility.Setup(x => x.Add(new DataStatus { Pin = "test" }, "data.com", "test")).Returns(Task.FromResult(true));
var dataManager = new DataManager(loggingService.Object, configuration.Object);
Assert.AreEqual(testContent, dataManager.GetStuff(testName)[0]);
}
Question:
How can I mock "GetData" and "GetClientValues" using Moq or any other library to return dummy data from those two methods? Is this possible to do?
I think you can use CallBase = true.
So your mock will behave like true class.
var dataManager = new Mock<DataManager>(loggingService.Object, configuration.Object) { CallBase = true };
dataManager.Object.Method1();
dataManager.Object.Method2();
And you can mock some methods like you do with utility.
Search by "CallBase" on page:
https://github.com/Moq/moq4/wiki/Quickstart
UPDATE
I made methods public to call them.
And there is some issue, that i didn't see last time. You need to make "virtual" methods if you want change their behavior.
There is example of code. As you can see, GetClientValues() returns real data because we call default real implementation with CallBase = true and GetData() returns dummy data because we mocked its behavior.
public class DataManager
{
public DataManager(ILoggingService loggingService, IConfiguration configuration)
{
// ...
}
// how to mock this to return dummy data?
public virtual DataHolder GetData()
{
return new DataHolder
{
Data = "RealData"
};
}
// how to mock this to return dummy data?
public IEnumerable<ProcessClient> GetClientValues(string value)
{
return new List<ProcessClient>
{
new ProcessClient
{
Data = "RealData"
}
};
}
}
And tests.
public class Tests
{
[SetUp]
public void Setup()
{
}
[Test]
public void Test1()
{
var loggingService = new Mock<ILoggingService>();
var config = new Mock<IConfiguration>();
var dataManager = new Mock<DataManager>(loggingService.Object, config.Object){CallBase = true};
dataManager.Setup(x => x.GetData()).Returns(new DataHolder { Data = "FakeData" });
Assert.AreEqual("RealData", dataManager.Object.GetClientValues("test").First().Data);
Assert.AreEqual("FakeData", dataManager.Object.GetData().Data);
}
}
But anyway i think, that this approach isn't good for testing.
There is a problem with testing private methods. We have some ways (for example Invoke()), but in general, "if you want to unit test a private method, something may be wrong". I would like to recommend you to read something about test design and writing testable code.
About testing private methods How do you unit test private methods?.
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);
}
}
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);
}
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
Is there in C# some kind of equivalent of ExpectedSystemExit in Java? I have an exit in my code and would really like to be able to test it. The only thing I found in C# is a not really nice workaround.
Example Code
public void CheckRights()
{
if(!service.UserHasRights())
{
Environment.Exit(1);
}
}
Test Code
[TestMethod]
public void TestCheckRightsWithoutRights()
{
MyService service = ...
service.UserHasRights().Returns(false);
???
}
I am using the VS framework for testing (+ NSubstitute for mocking) but it is not a problem to switch to nunit or whatever for this test.
You should use dependency injection to supply to the class being tested an interface that provides an environmental exit.
For example:
public interface IEnvironment
{
void Exit(int code);
}
Let's also assume that you have an interface for calling UserHasRights():
public interface IRightsService
{
bool UserHasRights();
}
Now suppose your class to be tested looks like this:
public sealed class RightsChecker
{
readonly IRightsService service;
readonly IEnvironment environment;
public RightsChecker(IRightsService service, IEnvironment environment)
{
this.service = service;
this.environment = environment;
}
public void CheckRights()
{
if (!service.UserHasRights())
{
environment.Exit(1);
}
}
}
Now you can use a mocking framework to check that IEnvironment .Exit() is called under the right conditions. For example, using Moq it might look a bit like this:
[TestMethod]
public static void CheckRights_exits_program_when_user_has_no_rights()
{
var rightsService = new Mock<IRightsService>();
rightsService.Setup(foo => foo.UserHasRights()).Returns(false);
var enviromnent = new Mock<IEnvironment>();
var rightsChecker = new RightsChecker(rightsService.Object, enviromnent.Object);
rightsChecker.CheckRights();
enviromnent.Verify(foo => foo.Exit(1));
}
Ambient contexts and cross-cutting concerns
A method such as Environment.Exit() could be considered to be a cross-cutting concern, and you might well want to avoid passing around an interface for it because you can end up with an explosion of additional constructor parameters. (Note: The canonical example of a cross cutting concern is DateTime.Now.)
To address this issue, you can introduce an "Ambient context" - a pattern which allows you to use a static method while still retaining the ability to unit test calls to it. Of course, such things should be used sparingly and only for true cross-cutting concerns.
For example, you could introduce an ambient context for Environment like so:
public abstract class EnvironmentControl
{
public static EnvironmentControl Current
{
get
{
return _current;
}
set
{
if (value == null)
throw new ArgumentNullException(nameof(value));
_current = value;
}
}
public abstract void Exit(int value);
public static void ResetToDefault()
{
_current = DefaultEnvironmentControl.Instance;
}
static EnvironmentControl _current = DefaultEnvironmentControl.Instance;
}
public class DefaultEnvironmentControl : EnvironmentControl
{
public override void Exit(int value)
{
Environment.Exit(value);
}
public static DefaultEnvironmentControl Instance => _instance.Value;
static readonly Lazy<DefaultEnvironmentControl> _instance = new Lazy<DefaultEnvironmentControl>(() => new DefaultEnvironmentControl());
}
Normal code just calls EnvironmentControl.Current.Exit(). With this change, the IEnvironment parameter disappears from the RightsChecker class:
public sealed class RightsChecker
{
readonly IRightsService service;
public RightsChecker(IRightsService service)
{
this.service = service;
}
public void CheckRights()
{
if (!service.UserHasRights())
{
EnvironmentControl.Current.Exit(1);
}
}
}
But we still retain the ability to unit-test that it has been called:
public static void CheckRights_exits_program_when_user_has_no_rights()
{
var rightsService = new Mock<IRightsService>();
rightsService.Setup(foo => foo.UserHasRights()).Returns(false);
var enviromnent = new Mock<EnvironmentControl>();
EnvironmentControl.Current = enviromnent.Object;
try
{
var rightsChecker = new RightsChecker(rightsService.Object);
rightsChecker.CheckRights();
enviromnent.Verify(foo => foo.Exit(1));
}
finally
{
EnvironmentControl.ResetToDefault();
}
}
For more information about ambient contexts, see here.
I ended up creating a new method which I can then mock in my tests.
Code
public void CheckRights()
{
if(!service.UserHasRights())
{
Environment.Exit(1);
}
}
internal virtual void Exit()
{
Environment.Exit(1);
}
Unit test
[TestMethod]
public void TestCheckRightsWithoutRights()
{
MyService service = ...
service.When(svc => svc.Exit()).DoNotCallBase();
...
service.CheckRights();
service.Received(1).Exit();
}
If your goal is to avoid extra classes/interfaces just to support tests, how do you feel about Environment.Exit action via Property Injection?
class RightsChecker
{
public Action AccessDeniedAction { get; set; }
public RightsChecker(...)
{
...
AccessDeniedAction = () => Environment.Exit();
}
}
[Test]
public TestCheckRightsWithoutRights()
{
...
bool wasAccessDeniedActionExecuted = false;
rightsChecker.AccessDeniedAction = () => { wasAccessDeniedActionExecuted = true; }
...
Assert.That(wasAccessDeniedActionExecuted , Is.True);
}