Testing Nunit with Mock. Interface instance

I have the following (simplified) code.

public class Controller { private readonly IService _service; public Controller(IService service) { _service = service; } public async Task<IHttpActionResult> Create(MyObject object) { var result = _service.method(object); if (!result.Succeeded) { return this.GetErrorResult(object); } } } 

and SimpleInjector is used to enter the dependency between _service and its implementation class, for example:

 public static void Register(Container container) { container.Register<IService, Service>(); } 

As a side note, injections and unit testing are new to me, so I don't understand them completely, but I'm learning.

If I run the application through Swagger, everything works fine.

As a note, the Register function is called when I launch the application through Swagger.

Now I am trying to configure some unit tests using NUnit, as well as Mocking for an IService object as follows:

 var Service = new Mock<IService>(); Controller _controller = new Controller(Service.Object); _controller.Create(new MyObject object()); 

which seems right to me so far - although I'm not sure?

The problem is that for unit test, the result always null - I think this is because there is a problem with my interface Mock - it does not seem to find a method - it never enters into it and does not display int he debugger.

As a side note, for the unit test, the Register method is not called. I tried to call him to register the addiction, but this does not help.

As I said above, this is all new to me, and I am on the verge of my understanding of all this.

I am out of ideas and do not know where to look from here, so any help would be greatly appreciated.

EDIT:

The original question was as follows:

 public async Task<IHttpActionResult> Create(string content) 

which I updated to:

 public async Task<IHttpActionResult> Create(MyObject object) 

Can anyone tell me how I can pass a generic link to MyObject in customization without instantiating this class.

So basically I want to say that an instance of this class will be passed without creating this instance.

I tried the following:

 Service.Setup(x => x.method(It.IsAny<MyObject>()) but it says cannot convert MethodGroup to MyObject 

and here is the definition of IService:

 public interface IService { IdentityResult method(ApplicationUser user, UserLoginInfo login); } 
+1
source share
3 answers

You need to configure the Mock object to return something for IService.method as follows:

 var Service = new Mock<IService>(); Service.Setup(x => x.method(It.IsAny<string>()) .Returns<string>(str => **whatever result you need**); 

With the addition of your actual IService definition, you should change the Setup call to:

 Service.Setup(x => x.method(It.IsAny<ApplicationUser>(), It.IsAny<UserLoginInfo>()) .Returns<ApplicationUser, UserLoginInfo>((user, login) => new IdentityResult(true)); 
+2
source

The installation method must be called on the Mock object.

 var Service = new Mock<IService>(); Service.Setup(x=>x.method("argument")).Returns(YourReturnObject) Controller _controller = new Controller(Service.Object); 
+3
source

Using a simplified example

 public class Controller { private readonly IService _service; public Controller(IService service) { _service = service; } public async Task<IHttpActionResult> Create(string content) { var result = await _service.method(content); if (!result.Succeeded) { return this.GetErrorResult(result); } return Ok(); } } 

Assume IService is defined as

 public interface IService { Task<Result> method(string input); } public class Result { public bool Succeeded { get; set; } } 

For unit test, you need to configure the layout to fake the actions needed for the test

 public async Task Controller_Given_Content_Should_Return_Ok() { //Arrange var input = "content"; var mockService = new Mock<IService>(); mockService .Setup(m => m.method(input)) .ReturnAsync(new Result { Succeeded = true }); var _controller = new Controller(mockService.Object); //Act var result = await _controller.Create(input); //Assert Assert.IsNotNull(result); Assert.IsInstanceOfType(result,typeof(OkResult)); } 

Given that the test method is asynchronous, you also want to configure the test as asynchronous.

+2
source

Source: https://habr.com/ru/post/1014182/


All Articles