Assert that method has been called using xUnit - c#

I have a class with a logging method that I want to test. For the example I want to check if the Console.WriteLine method has been called. This is my sample class
public class MyClass
{
public void LogSomething()
{
Console.WriteLine("Test");
}
}
and its test
public class MyClassTests
{
[Fact]
public void LogsSomething()
{
MyClass myClass = new MyClass();
myClass.LogSomething(); // Assert that Console.WriteLine has been called once
}
}
Is there something I can use? (Preferably without additional packages)
I am looking for assertions like this (Pseudo code)
Assert.Method(Console.WriteLine).ToHaveBeenCalledWith(myClass.LogSomething);
Assert.Method(Console.WriteLine).ToHaveBeenCalledWith(myClass.LogSomething).Times(3); // Check if Console.WriteLine has been called 3 times (loop inside the LogSomething method)

I think you cannot assert that out of the box.
Your best bet is using Moq or another mock framework to do something along this lines.
You should always aim for a decoupled logic using dependency injection, otherwise you will end up having a tightly coupled code that is difficult to test and will not be easy to refactor whenever a change in requirements arrives
public interface ILogger
{
public void Log();
}
public class Logger: ILogger
{
public void Log()
{
Console.WriteLine("Look mom, i'm logging");
}
}
public class MyClass
{
private readonly ILogger Logger
public MyClass(ILogger logger)
{
Logger = logger;
}
public void MyMethod()
{
//other code
Logger.Log();
}
}
public class MyClassTests
{
[Fact]
public void LogsSomething()
{
//arrange
var logger = new Mock<ILogger>();
//act
var sut = new MyClass(logger.Object);
sut.MyMethod();
//Assert
logger.Verify(foo => foo.Log(), Times.Once()); //here
//some other assertions
}
}

Related

Testing MassTransit consumer with mediator

I have consumer CarCreatedConsumer which I want to unit test
public class CarCreatedConsumer: IConsumer<CarCreatedEvent>
{
private readonly IMediator _mediator;
public CarCreatedConsumer(IMediator mediator)
{
_mediator = mediator;
}
public async Task Consume(ConsumeContext<CarCreatedEvent> context)
{
....
}
}
Using MassTransit.Testing I'm trying to write test for event consumer
[TestFixture]
public class MyTests
{
private ServiceProvider _provider;
private InMemoryTestHarness _harness;
[SetUp]
public void SetUp()
{
_provider = new ServiceCollection()
.AddMassTransitInMemoryTestHarness(cfg =>
{
cfg.AddConsumer<CarCreatedConsumer>();
}).AddMediator()
.BuildServiceProvider(true);
_harness = _provider.GetRequiredService<InMemoryTestHarness>();
}
[Test]
public async Task MessageShouldBeConsumed()
{
await _harness.Start();
try
{
await _harness.InputQueueSendEndpoint.Send<CarCreatedEvent>(new
{
CarId = Guid.NewGuid.ToString(),
CarOwnerName = "John Stuart"
...
}
// Always false
Assert.True(_harness.Consumed.Select<CarCreatedEvent>().Any());
}
finally
{
await _harness.Stop();
}
}
}
I'm expect this to return true but it fails (returns false). Obviously I'm doing something wrong, any ideas?
Your test harness usage is suspect, and is also using an obsolete version of the configuration. I'd suggest reading the documentation on the current test harness and how to test your consumer.

How do I generate an Exception for this test?

I am trying to test the Exception handler in my application.
However I can't generate an Exception for the Constructor.
Normally, I would create a moq of an Object and then do a setup where a call to Object.method throws an Exception. Then simply detect the Exception in the Test.
However, in this Constructor the only call is:
CredentialProfileStoreChain.TryGetAWSCredentials
CredentialProfileStoreChain.TryGetAWSCredentials can't be overridden so I can't use moq Setup to generate an Exception.
Code:
public class AWSDynamoDbManager : IAWSDynamoDbManager
{
private readonly ILogger _logger;
private readonly AmazonDynamoDBClient _dynamoDbClient;
//NOTE: This setting is in the app.config of the calling application so that different uses can use different profiles
private readonly string _awsProfileName = ConfigurationManager.AppSettings["AWSProfileName"];
public AWSDynamoDbManager(CredentialProfileStoreChain chain, ILogger logger)
{
this._logger = logger;
try
{
AWSCredentials awsCredentials;
chain.TryGetAWSCredentials(_awsProfileName, out awsCredentials);
_dynamoDbClient = new AmazonDynamoDBClient(awsCredentials, RegionEndpoint.EUWest2);
}
catch (Exception e)
{
logger.Error("Could Not Open DynamoDB");
logger.Error("Error: " + e.Message);
throw;
}
}
}
Test:
public void TestToSeeIfWeCatchTheExceptionIfWeCannotConnectToTheDatabase()
{
// arrange
var mockLogger = new Mock<ILogger>();
var mockChain = new Mock<CredentialProfileStoreChain>();
// act / assert
Assert.Catch<ArgumentException>(() => new AWSDynamoDbManager(mockChain.Object, mockLogger.Object));
}
What can I use to force the Constructor to cause an Exception?
when my hands are tied because of an external dependency or static type, i use a wrapper, so that's what i'd use here. since we can't mock a CredentialProfileStoreChain, we throw it in a wrapper and use the wrapper.
public interface ICredentialProfileStoreChainWrapper
{
void TryGetAWSCredentials(/*TODO*/);
}
public class CredentialProfileStoreChainWrapper
{
readonly CredentialProfileStoreChain _Chain;
public CredentialProfileStoreChainWrapper(CredentialProfileStoreChain chain)
{
_Chain = chain;
}
public void TryGetAWSCredentials(/*TODO*/)
{
_Chain.TryGetAWSCredentials(/*TODO*/);
}
}
public class AWSDynamoDbManager : IAWSDynamoDbManager
{
public AWSDynamoDbManager(ICredentialProfileStoreChainWrapper chainWrapper, ILogger logger)
{
//TODO
chainWrapper.TryGetAWSCredentials(/*TODO*/);
}
}
public class Tests
{
[Test]
public void TestToSeeIfWeCatchTheExceptionIfWeCannotConnectToTheDatabase()
{
var wrapper = new Mock<ICredentialProfileStoreChainWrapper>();
var logger = new Mock<ILogger>();
var manager = new AWSDynamoDbManager(wrapper.Object, logger.Object);
wrapper.Setup(s => s.TryGetAWSCredentials(/*TODO*/)).Throws(new Exception());
//TODO
}
}

Mocked method do not pass correct value

I am trying to understand how mocking works in Xunit with AutoFixture. I have created Service and Repository classes and their interfaces. Mocked method should pass value which is different from default value.
Mocked method always pass default values instead of values which i am writing in ".Returns()". I have tried AutoConfiguredMoqCustomization but it provides completely random values which i can't get back.
Repository.cs
public class Repository : IRepository
{
public int GetInt()
{
return 999;
}
}
Service.cs
public class Service : IService
{
private readonly Repository _repository;
public Service()
{
_repository = new Repository();
}
public string GetStringFromInt()
{
return _repository.GetInt().ToString();
}
}
Test
[Fact]
public void Test()
{
var fixture = new Fixture().Customize(new AutoMoqCustomization());
var repositoryMock = fixture.Create<Mock<IRepository>>();
var service = fixture.Create<Service>();
repositoryMock.Setup(x => x.GetInt()).Returns(1);
var act = service.GetStringFromInt();
Assert.Equal("1", act);
}
As you see value by default in Repository is 999 and I am expecting 1 from repositoryMock but result is "999" instead of "1".
Ow I have understood my problem. When I declare parameters with auto moq testing service must be AFTER all mocked repositories
Test
[Theory, AutoMoqData]
public void Test([Frozen] Mock<IRepository> repositoryMock, Service service)
{
...
}
Attribute
public class AutoMoqDataAttribute : AutoDataAttribute
{
public AutoMoqDataAttribute() : base(GetDefaultFixture)
{
}
private static IFixture GetDefaultFixture()
{
return new Fixture().Customize(new AutoMoqCustomization());
}
}
You should freeze your mock first. When you call Create on AutoFixture, it will create you a new instance every time. Try the following in the modified code (where you are using an interface of the type in your constructor).
public class ServiceTests
{
private readonly IFixture fixture = new Fixture().Customize(new AutoMoqCustomization());
public ServiceTests()
{
fixture.Register<IService>(() => fixture.Create<Service>());
}
[Fact]
public void Test()
{
// Arrange
var repositoryMock = fixture.Freeze<Mock<IRepository>>();
repositoryMock.Setup(x => x.GetInt()).Returns(1);
var service = fixture.Create<IService>();
// Act
var act = service.GetStringFromInt();
// Verify
Assert.Equal("1", act);
}
}
To check that you have set up autofixture correctly, you can try the following in the unit test in future.
var repo1 = fixture.Create<IRepository>();
var repo2 = fixture.Create<IRepository>();
Assert.Equal(repo1.GetHashCode(), repo2.GetHashCode());
If the above fails, that indicates that you have not frozen a type. These lines of code have saved me so much head scratching in the past...
You are doing DI wrong, you are not injecting Repository into Your serice.
Try like this.
public class Repository : IRepository
{
public int GetInt()
{
return 999;
}
}
public class Service : IService
{
IRepository Repository;
public Service(IRepository repository)
{
this.Repository = repository;
}
public string GetStringFromInt()
{
return Repository.GetInt().ToString();
}
}
Now when you mock IRepository, you can add it to Service.
You are using a new Repository() in constructor, so you are using that implementation

Moq: setup a generic method with mocked parameters

I've been trying to write a few tests in NUnit for my generic methods, without success. I hope I can make my situation clear, since I had to heavily paraphrase my code.
DoBusinessLogic() is the method I want to test. It calls two other methods from the base class:
public class MySvc : BaseSvc, IMySvc
{
private readonly IMyRepo _repo;
private readonly IMyConnectorClass _connector
public MySvc(IMyRepo repo) : base(repo)
{
_repo = repo;
_connector = _repo.GetConnector();
}
public async Task DoBusinessLogic(int id1, int id2){
bool doesFirstObjectExist = await CheckMainObjExists<Foo>(id1, _connector);
await CheckSubObjExists<Bar>(id2, _connector);
// further business logic
}
}
Those methods, in turn, call the same repository method, but have different logic after it:
public abstract class BaseSvc : IBaseSvc
{
private readonly IBaseRepo _repo
protected BaseSvc(IBaseRepo repo)
{
_repo = repo;
}
protected async Task<bool> CheckMainObjExists<T>(int? id, IMyConnectorClass connector)
{
return await _repo.GetObjectByIdAsync<T>(Id, connector) != null;
}
protected async Task CheckSubObjExists<T>(int? id, IMyConnectorClass connector)
{
if (await _repo.GetObjectByIdAsync<T>(Id, connector) == null)
{ throw new Exception("Object not found!"); }
}
}
Next, I want to write unit a test for DoBusinessLogic() in the MySvc class. Unfortunately, it seems I can't mock the responses from the repository.
[TestFixture]
public class MySvcTests
{
private MySvc _svc;
private Mock<IMyRepo> _repoMock;
private Mock<IMyConnectorClass> _connectorMock;
[SetUp]
public void SetUp()
{
_repoMock = new Mock<IMyRepo>() {};
_connectorMock = new Mock<IMyConnectorClass>();
_repo.SetUp(r => r.GetConnector()).Return(_connectorMock.Object);
_svc = new MySvc(_repoMock);
}
/*
My intent in this test, is to make CheckMainObjExists() pass,
but make CheckSubObjExist() fail.
*/
[Test]
public async Task DoBusinessLogic_If2ndObjectNotExist_ThrowException()
{
// This should return an object
_repoMock.Setup(r => r.GetObjectByIdAsync<Foo>(It.IsAny<int>(), _connectorMock.Object))
.ReturnsAsync(new Foo());
// This should return null
_repoMock.Setup(r => r.GetObjectByIdAsync<Bar>(It.IsAny<int>(), _connectorMock.Object))
.ReturnsAsync((Bar) null);
Assert.Throws<Exception>(await _svc.DoBusinessLogic());
}
}
However, when I run the test, both methods that I set up for my mock repo return null, whereas I expect a "true" from the first.
I do not know where the problem is situated, but I have my suspicions:
Is it possible to setup a method, using a mocked object as a parameter? In this case, is it possible to use _connectorMock.Object as a setup parameter?
Is it possible to setup the same generic method multiple times, but for a different type each time? It's first setup for Foo, then for Bar.
I just tested this code and it runs as expected. Now I had to make a lot of assumptions just to get the code to compile and run which would mean that my test of your code is flawed as I may have fixed something that was omitted in your example.
I made no changes to your test setup code, which worked.
[TestClass]
public class MySvcTests {
[TestMethod]
[ExpectedException(typeof(Exception))]
public async Task DoBusinessLogic_If2ndObjectNotExist_ThrowException() {
var _repoMock = new Mock<IMyRepo>() { };
var _connectorMock = new Mock<IMyConnectorClass>();
_repoMock.Setup(r => r.GetConnector()).Returns(_connectorMock.Object);
var _svc = new MySvc(_repoMock.Object);
// This should return an object
_repoMock.Setup(r => r.GetObjectByIdAsync<Foo>(It.IsAny<int>(), _connectorMock.Object))
.ReturnsAsync(new Foo());
// This should return null
_repoMock.Setup(r => r.GetObjectByIdAsync<Bar>(It.IsAny<int>(), _connectorMock.Object))
.ReturnsAsync((Bar)null);
await _svc.DoBusinessLogic(0, 0);
}
}

How to use Moq to mock and test an IService

I have a service set up as follows:
public interface IMyService
{
void AddCountry(string countryName);
}
public class MyService : IMyService
{
public void AddCountry(string countryName)
{
/* Code here that access repository and checks if country exists or not.
If exist, throw error or just execute. */
}
}
Test.cs
[TestFixture]
public class MyServiceTest
{
[Test]
public void Country_Can_Be_Added()
{ }
[Test]
public void Duplicate_Country_Can_Not_Be_Added()
{ }
}
How do I test AddCountry and moq the repository or service. I'm really not sure what to do here or what to mock. Can someone help me out?
Frameworks I'm using:
NUnit
Moq
ASP.NET MVC
And why would you need to use moq? You don't need to mock IService. In your case you can write your test like this:
[Test]
public void Country_Can_Be_Added()
{
new MyService().AddCountry("xy");
}
[Test]
public void Duplicate_Country_Can_Not_Be_Added()
{
Assert.Throws<ArgumentException>(() => new MyService().AddCountry("yx"));
}
You would need to mock the IRepository if you had a scenario like this:
interface IRepository { bool CanAdd(string country); }
class MyService : IService
{
private IRepository _service; private List<string> _countries;
public IEnumerable<string> Countries { get { return _countries; } }
public X(IRepository service) { _service = service; _countries = new List<string>(); }
void AddCountry(string x)
{
if(_service.CanAdd(x)) {
_conntires.Add(x);
}
}
}
And a test like this:
[Test]
public void Expect_AddCountryCall()
{
var canTadd = "USA";
var canAdd = "Canadd-a";
// mock setup
var mock = new Mock<IRepository>();
mock.Setup(x => x.CanAdd(canTadd)).Returns(false);
mock.Setup(x => x.CanAdd(canAdd)).Returns(true);
var x = new X(mock.Object);
// check state of x
x.AddCountry(canTadd);
Assert.AreEqual(0, x.Countires.Count);
x.AddCountry(canAdd);
Assert.AreEqual(0, x.Countires.Count);
Assert.AreEqual(0, x.Countires.Count);
Assert.AreEqual(canAdd, x.Countires.First());
// check if the repository methods were called
mock.Verify(x => x.CanAdd(canTadd));
mock.Verify(x => x.CanAdd(canAdd));
}
You test the concrete MyService. If it takes a dependency (say on IRepository) you would create a mock of that interface and inject it into the service. As written, no mocks are needed to test the service.
The point of creating the IMyService interface is to test other classes that depend on MyService in isolation. Once you know Repository works, you don't need to test it when you're testing MyService (you mock or stub it out). Once you know MyService works, you don't need to test it when you're testing MySomethingThatDependsOnMyService.

Categories

Resources