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