Control doesn't go to the virtual method while debugging - c#

I have a class with few methods. For these methods, I'm creating Unit Test cases using MSTest.
public class Class1
{
public virtual async Task<bool> GetData()
{
string var1 ="dsfs", var2 ="eer";
bool retVal = await DoSomething(var1, var2);
// some business logic
return true;
}
public virtual async Task<bool> DoSomething(string var1, string var2)
{
return true;
}
}
The test cases for method GetData() looks like this.
[TestClass()]
public class Class1Test
{
[TestMethod()]
public async Task GetDataTest()
{
var mockService = new Mock<Class1>();
var isTrue = ReturnTrue(); // this function returns true
mockService.Setup(x => x.DoSomething(It.IsAny<string>(), It.IsAny<string>())).Returns(isTrue);
var result = await mockService.Object.GetData();
Assert.IsTrue(result);
}
}
Now, the problem I'm facing is,
var result = await mockService.Object.GetData();
From this line the control doesn't go to the actual GetData() method in Class1. If I remove virtual keyword from the method definition, then the control goes to the method, i.e, if I make the method like this.
public async Task<bool> GetData()
{
bool retVal = await DoSomething(var1, var2);
// some business logic
return true;
}
I need to make this method virtual because, this method is called in some other method say "XYZMethod" and for writing test case "XYZMethod", I had mocked GetData() method there.
So is there anyway I can solve this problem without removing virtual keyword.
PS: Writing interfaces is not an option as this is a very heavy code which I'm working on and introducing Interface at this stage is not practical.

Enable CallBase on the mock so that it will invoke base members that have not been overridden by a setup expectation.
Also use ReturnsAsync when configuring asynchronous mocked members to allow them to flow correctly when invoked.
[TestClass()]
public class Class1Test {
[TestMethod()]
public async Task GetDataTest() {
//Arrange
var mockService = new Mock<Class1>(){
CallBase = true
};
var expected = ReturnTrue(); // this function returns true
mockService
.Setup(x => x.DoSomething(It.IsAny<string>(), It.IsAny<string>()))
.ReturnsAsync(expected);
//Act
var actual = await mockService.Object.GetData();
//Assert
Assert.IsTrue(actual);
}
}

Related

Make ProcessCmdKey async await - C# [duplicate]

I am overriding a method in a base class library. However, inside my overridden implementation I am using the new HttpClient which is all based on async methods. I therefore have to mark my method as async, which means that I need to change the return parameter of the method from string to Task. The compiler however gives an error: "The return type must be 'string' to match overridden member ...."
public class BaseClass
{
public virtual string GetName()
{
...
}
}
public class MyClass : BaseClass
{
public override async Task<string> GetName()
{
HttpClient httpClient = new HttpClient();
var response = await httpClient.GetAsync("");
if (response.IsSuccessStatusCode)
{
var responseContent = response.Content;
return await responseContent.ReadAsStringAsync();
}
return null;
}
}
Of course the obvious solution would be to change the return type of GetName() in BaseClass to Task<string>, but I have no control over BaseClass as it is an external library;
My current solution is to use the HttpClient classes in a synchronous fashion, i.e. change MyClass as follows:
public class MyClass : BaseClass
{
public override string GetName()
{
HttpClient httpClient = new HttpClient();
var response = httpClient.GetAsync("");
if (response.Result.IsSuccessStatusCode)
{
var responseContent = response.Result.Content;
return responseContent.ReadAsStringAsync()
.Result;
}
return null;
}
}
Is there any other way to do this?
Unfortunately there isn't a good solution here. There is no way to override a non-async method with an async one. I think your best bet is to have an async non-override method and call into that from the non-async one:
public class MyClass : BaseClass
{
public override string GetName()
{
return GetNameAsync().Value;
}
public async Task<string> GetNameAsync()
{
...
}
}
Note that this can cause problems though. If the original code didn't expect for any async code to be executing introducing this pattern could break expectations. I would avoid it if possible.
Luckily the ReadAsStringAsync().Result is not causing a deadlock since it is likely to have ConfigureAwait(false) within.
To prevent a deadlock, you could use one of the following methods:
public static T GetResult<T>(Func<Task<T>> func)
{
var httpContext = HttpContext.Context;
var proxyTask = Task.Run(() =>
{
HttpContext.Context = httpContext;
return func();
});
return proxyTask.Result;
}
// or
public static T GetResult<T>(Func<Task<T>> func)
{
var syncContext = SynchronizationContext.Current;
SynchronizationContext.SetSynchronizationContext(null);
var task = func();
SynchronizationContext.SetSynchronizationContext(syncContext);
return task.Result;
}
This way you would call
public override string GetName()
{
...
return GetResult(() => responseContent.ReadAsStringAsync());
...
}
The former has a performance overhead by spawning a new thread, while the latter suffers from breaking SynchronizationContext flow, which makes any context bound to it unavailable in the task being called, e.g. HttpContext.Current.
I've also had this problem, and the solution was using an interface, in which 'async' isn't part of the method's signature.
public abstract class Base : IInvokable {
/* Other properties ... */
public virtual async Task Invoke() {
/*...*/
}
}
public interface IInvokable {
Task Invoke();
}
public class Derived
{
public override async Task Invoke() {
// Your code here
}
}

How to get result of method called inside of a mocked class

There is a way to get the result returned from a method called into a mocked object?
For example, if I have the following classes and an interface:
DoSomethingClass.cs
public class DoSomethingClass
{
IInternalClass _intClass;
public DoSomethingClass(IInternalClass intClass)
{
_intClass = intClass;
}
void DoSomething(int number)
{
// do some transformation to the number, e.g.
var transformedNumber = number * (number - 1);
var x = _intClass.IsEven(transformedNumber);
// do something else
}
}
InternalClass.cs
public interface IInternalClass
{
bool IsEven(int number);
}
public class InternalClass: IInternalClass
{
public InternalClass();
public bool IsEven(int number)
{
return number % 2 == 0;
}
}
And a test class:
Tests.cs
public class Tests
{
[Fact]
public async Task test_something()
{
//PREPARE
var request = 10
var internalMock = new Mock<IInternalClass>();
var classToTest = new DoSomethingClass(internalMock.Object);
// ACT
await classToTest.DoSomething(request);
//ASSERT
//Here I want to check if the method IsEven of the internal class return the right result
}
}
}
What I want to assert is the return value of the method IsEven of the InternalClass called inside the main method DoSomething, there is no way that I can know the transformedNumber calculated inside this method and passed to IsEven.
NOTE: I use the Moq library to mock object.
Excuse me for the stupid example above, but I don't have any simple real code to show here, however I hope this can be sufficient to understand the question.
I am assuming that you want to verify that your IsEven method is called on the mocked IInternalClass?
If that is the case then you can use the Verify method on the mock as follows:
[Fact]
public void DoSomething_Verify()
{
var request = 10;
var internalMock = new Mock<IInternalClass>();
var classToTest = new DoSomethingClass(internalMock.Object);
classToTest.DoSomething(request);
//Verify that the mock is invoked with the expected value, the expected number of times
internalMock.Verify(v => v.IsEven(90), Times.Once);
//There are lots of other options for Times, e.g. it is never called with an unexpected value maybe.
internalMock.Verify(v => v.IsEven(91), Times.Never);
}
Also to call the DoSomething method with await you would need to change the method signature as follows:
public async Task DoSomethingAsync(int number)
{
// do some transformation to the number, e.g.
var transformedNumber = number * (number - 1);
var x = _intClass.IsEven(transformedNumber);
// do something else
}
Then you can create a unit test like this:
[Fact]
public async void DoSomething_VerifyAsync()
{
var request = 10;
var internalMock = new Mock<IInternalClass>();
var classToTest = new DoSomethingClass(internalMock.Object);
//To call the DoSomething method with await the method needs the async Task and a good convention is to append Async to the name
await classToTest.DoSomethingAsync(request);
//Another slightly more generic option is to verify that the mock was called with and int exactly n Times
internalMock.Verify(v => v.IsEven(It.IsAny<int>()), Times.Exactly(1));
}

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);
}
}

Moq with Task await

Since I have converted my WCF methods to Async, my unit tests have failed, and I can't figure out the correct syntax to get them to work.
Cllient proxy class
public interface IClientProxy
{
Task DoSomething(CredentialDataList credentialData, string store);
}
service class
public class CredentialSync : ICredentialSync
{
private ICredentialRepository _repository;
private IClientProxy _client;
public CredentialSync()
{
this._repository = new CredentialRepository();
this._client = new ClientProxy();
}
public CredentialSync(ICredentialRepository repository, IClientProxy client)
{
this._repository = repository;
this._client = client;
}
public async Task Synchronise(string payrollNumber)
{
try
{
if (string.IsNullOrEmpty(payrollNumber))
{
.... some code
}
else
{
CredentialDataList credentialData = new CredentialDataList();
List<CredentialData> credentialList = new List<CredentialData>();
// fetch the record from the database
List<GetCredentialData_Result> data = this._repository.GetCredentialData(payrollNumber);
var pinData = this._repository.GetCredentialPinData(payrollNumber);
// get the stores for this employee
var storeList = data.Where(a => a.StoreNumber != null)
.GroupBy(a => a.StoreNumber)
.Select(x => new Store { StoreNumber = x.Key.ToString() }).ToArray();
var credential = this.ExtractCredentialData(data, pinData, payrollNumber);
credentialList.Add(credential);
credentialData.CredentialList = credentialList;
foreach (var store in storeList)
{
//this line causes an Object reference not set to an instance of an object error
await _client.DoSomething(credentialData, store.StoreNumber);
}
}
}
catch (Exception ex)
{
throw new FaultException<Exception>(ex);
}
}
Test Class
/// </summary>
[TestClass]
public class SynchTest
{
private Mock<ICredentialRepository> _mockRepository;
private Mock<IClientProxy> _mockService;
[TestInitialize]
public void Setup()
{
... some setups for repository which work fine
}
[TestMethod]
public async Task SynchroniseData_WithOneEmployee_CallsReplicateService()
{
this._mockService = new Mock<IClientProxy>();
this._mockService.Setup(x=>x.DoSomething(It.IsAny<CredentialDataList>(), It.IsAny<string>()));
// arrange
string payrollNumber = "1";
CredentialSync service = new CredentialSync(this._mockRepository.Object, this._mockService.Object);
// act
await service.Synchronise(payrollNumber);
// assert
this._mockService.VerifyAll();
}
The error is when ClientProxy.DoSomething is called:
Object reference not set to an instance of an object
The parameters are both fine.
If I convert my ClientProxy.DoSomething method to a synchronous method
(public void DoSomething(...) )the code works fine, but I do need this to be called asynchronously
DoSomething returns null instead of returning a Task, and so you get an exception when awaiting it. You need to specify when building the mock that it should return a Task.
In this case it seems that you can simply return an already completed task using Task.FromResult so the mock setup should look like this:
this._mockService.Setup(...).Returns(Task.FromResult(false));
Beginning with the next version of .Net (4.6) you can use Task.CompletedTask like this:
this._mockService.Setup(...).Returns(Task.CompletedTask);
You can reduce the amount of clutter in the code by using ReturnsAsync
this._mockService.Setup(...).ReturnsAsync(false);
This way you can remove the Task.FromResult part of the code
I think you need to return the Task from the DoSomething mock
this._mockService.Setup(x => x.DoSomething(It.IsAny<CredentialDataList>(), It.IsAny<string>()))
.Returns(Task.FromResult<int>(0));

interaction unit testing using Moq

I have a testing class
public class TerminationRequestValidation : ValidatorBase<TerminationRequest>
{
public TerminationRequestValidation(IIntHR2BLLContext context) : base(context)
{
}
public override ValidationResult ValidateWithoutThrow(TerminationRequest request)
{
var result = ValidationResult.Success;
/* some logic */
var isHRIAdvanced = Context.Logics.Accessible.HasAccess(request, IntHRSecurityOperationCode.TerminationRequestSetTerminationDateBehindhand);
if (!isHRIAdvanced && Context.Logics.Termination.IsTerminationDateChanged(request))
{
result += CheckTerminationDate(request);
}
return result;
}
public virtual ValidationResult CheckTerminationDate(TerminationRequest request)
{
var result = ValidationResult.Success;
/* any validation logic */
return result;
}
}
I need to check 'CheckTerminationDate' method is performed
[TestMethod]
public void Validate_TerminationDateChangedbyNotAdvanced_TerminationDateCheck()
{
var context = FakeContext.Create();
// first stub
var accessibleBllStub = new Mock<IAccessibleBLL>(MockBehavior.Loose);
accessibleBllStub.Setup(z => z.HasAccess(It.IsAny<TerminationRequest>(), It.IsAny<IntHRSecurityOperationCode>()))
.Returns<TerminationRequest, IntHRSecurityOperationCode>((x, y) => y != IntHRSecurityOperationCode.TerminationRequestSetTerminationDateBehindhand);
context.StubBLL(z => z.Accessible, accessibleBllStub.Object);
// second stub
var terminationBLLStub = new Mock<ITerminationBLL>(MockBehavior.Loose);
terminationBLLStub.Setup(z => z.IsTerminationDateChanged(It.IsAny<TerminationRequest>())).Returns(true);
context.StubBLL(z => z.Termination, terminationBLLStub.Object);
// mock
var validator = new Mock<TerminationRequestValidation>(MockBehavior.Loose, context.MainContext);
// act
validator.Object.ValidateWithoutThrow(termination);
//assert
validator.Verify(z => z.CheckTerminationDate(It.IsAny<TerminationRequest>()));
}
This unit test off course isn't work. On the one hand I need to call real 'ValidateWithoutThrow' method, on the another hand I need to check that stub method 'CheckTerminationDate' is performed.
Guys, help me to find the best solution! May be I need to redesign testing class to make in more testable
I need to check 'CheckTerminationDate' method is performed
You don't. You need to test that the request was validated. Whether that's done inline, or by calling CheckTerminationDate, or by calling some other method, that's an implementation detail - and unit tests don't care about that.
So, your tests should look something like this:
public void ValidateWithoutThrow_ReturnsSucessfulResult_When_RequestIsValid()
{
var validRequest = //...
var validator = new TerminationRequestValidation(/*...*/); // don't mock this class
var result = validator.TerminationRequestValidation(validRequest);
Assert.Equal(ValidationResult.Success, result);
}
public void ValidateWithoutThrow_ReturnsUnsucessfulResult_When_RequestIsInvalid()
{
var invalidRequest = //...
var validator = new TerminationRequestValidation(/*...*/); // don't mock this class
var result = validator.TerminationRequestValidation(invalidRequest);
Assert.NotEqual(ValidationResult.Success, result);
}
As a general rule of thumb, avoid verifying how the method works internally. You're coupling your tests to implementation details and refactoring/maintaining those details will be a living hell.

Categories

Resources