Unit Testing FluentEmail calls with C# NUnit
I'm trying to write unit tests for an API method which calls the FluentEmail .Send() method. However, the tests are failing. The error I am getting is ArgumentNullException with 'Value cannot be null'. When I debugged, the exception was getting thrown at the FluentEmail call.
Could you help with why the exception gets thrown?
Thanks
Are you setting up a default sender? It might be helpful to create a UnitTestSender that just adds the emails to some list you can check in your test?
@phaniallamsetty are you still experiencing this issue? As a thought, email service calls seem like an external dependancy - by which I mean : If you were to run this test code on a machine with no internet access, then the code would fail.
I realise that - and I'm guilty of this - we sometimes use unit test as a synonym for automated testing, and that what you're testing is probably best described as integration testing.
But if you are wanting to unit test this API call, then what you might be better doing is mocking the Send Method, and asserting that it is called with the correct values.
@mickeyr 's suggestion to set up a UnitTestSender would be a possible solution - but I think that would mean adding code to the API just to test it, whereas I think we can use something like moq to allow us to test the production code without needing to alter it.
If it is helpful I can try to create a small API and unit test that would demonstrate this approach.
@phaniallamsetty - I have a sample project here based on a .NET Core WebAPI, which mocks the FluentEmail interface, allowing you to test that it has been called with expected parameters.
https://github.com/computamike/mocking-fluent-email
For integration tests wouldn't it be better to mock the sender so you test all the fluent stuff working as-is?