How to mock method that has return type from of the (bool, string)?

Advertisements The following function has a return value of (bool, string). How to Moq it to be ready for unit testing. Task<(bool, string)> CreatePhoneCertificate( int orderId, CancellationToken cancellationToken); var mockInsuranceService = new Mock<IInsuranceService>(); mockInsuranceService.Setup(x => x.CreatePhoneCertificate(12345, It.IsAny<CancellationToken>())).ReturnsAsync(???); Thank you >Solution : Just create a value tuple with desired values: mockInsuranceService.Setup(x => x.CreatePhoneCertificate(12345, It.IsAny<CancellationToken>())) .ReturnsAsync((true, "someString"));… Read More How to mock method that has return type from of the (bool, string)?

How to unit test methods which depend on a private field?

Advertisements I just started writing some tests for a new project and encountered the following "problem": I mocked the dependent INotifyVariableChangedService and OpcClient to test my OpcService: private readonly OpcUaClient _client; private readonly INotifyVariableChangedService _notifyVariableChangedService; private readonly ushort _symbolicsIndex; public OpcService(INotifyVariableChangedService notifyVariableChangedService) { this._notifyVariableChangedService = notifyVariableChangedService; this._client = new OpcUaClient("", ""); _symbolicsIndex = _client.GetSymbolicsNameSpaceId(); }… Read More How to unit test methods which depend on a private field?

How to deserialize C# async OkResult from controller that uses mongodb

Advertisements I’m trying to create xUnit using mock tests for my api that uses DataAccess layer for mongodb. But for some reason it just doesn’t parse okResult to Json or Bson. I definitely know that there is 2 values that I need from debugger: My model is: namespace DataAccess.Models; [BsonIgnoreExtraElements] public class CyberGood { [BsonId]… Read More How to deserialize C# async OkResult from controller that uses mongodb

Cannot test for exceptions in F# using xUnit

Advertisements Here is the code: module Tests open System open Xunit [<Fact>] let “Simple Test“ () = Assert.Throws<Exception>(failwith "Error") This fails to compile with: error FS0041: A unique overload for method ‘Throws’ could not be determined based on type information prior to this program point. A type annotation may be needed. Known type of argument:… Read More Cannot test for exceptions in F# using xUnit

How Can I Setup mock object for unit test with Moq and Xunit in ASP.NET MVC Application?

Advertisements I am new to unit testing and was trying to write unit tests for controllers’ action methods using Moq Library and Xunit framework. I created a list of departments as mock data and tried testing it by passing in Returns() method with the Setup() method of the Moq. But it shows error "Cannot convert… Read More How Can I Setup mock object for unit test with Moq and Xunit in ASP.NET MVC Application?

Is it possible to pass number of times invocation is met as parameter to a unit test class method?

Advertisements I have a unit test class method that currently takes 1 parameter but I want to extend it to receive 2 parameters with the latter being the number of times invocation is met on a mock object. What I currently have is something like this, which doesn’t compile successfully due to errors [Theory] [InlineData("",… Read More Is it possible to pass number of times invocation is met as parameter to a unit test class method?

Error while testing my async function using XUNIT in c# .net core

Advertisements I have convereted my IEnumerable function into a (public async Task<List>) however im having issues fixing my unit test for that specific part. Im using fakeiteasy to mock my data, and assert that my result.count == 1 after fetching my data. However i get this specific error Unable to cast object of type ‘System.Threading.Tasks.Task1[System.Collections.Generic.List1[WebAPI__CodeFirst.Classes.Customer]]’… Read More Error while testing my async function using XUNIT in c# .net core

Enum validation

Advertisements Purpose is to validate Enum. namespace Test.Enums { public enum Type { Audi, Porsche, Peugeot } } I would like to write these values into an array so that I can compare them. [Fact] public void Test() { //arrange string[] expected = {"Audi", "Porsche", "Peugeot"}; string[] actual = new Test.Enums.Type(); //assert Assert.Equal(expected, actual); }… Read More Enum validation

How to correctly run an async thread in a test method?

Advertisements I have this test that needs to run the RunWebSocket thread to make sure the SendAsync was received by my websocket. This works, but I am getting compiler warning CS4014. Of course, I do not like warnings, so instead of ignoring it, I want to do it "right". [Fact] public async void CabinetTagPushed_ShouldSendBroadcastToWebSocket() {… Read More How to correctly run an async thread in a test method?