ssannandeji / Zenject-2019

Dependency Injection Framework for Unity3D
MIT License
2.53k stars 363 forks source link

Set up moq unit test multiple times #635

Closed PhilippCh closed 5 years ago

PhilippCh commented 5 years ago

This is more of a general unit-testing question, but I don't quite know how to deal with it within the zenject context.

Assuming I have a class Service that is a dependency of my test subject. Within two different test (methods) I want to have Service.GetUsers() return two different results because I want to test how my subject reacts to them. In regular unit testing with Moq I'd just set up a new mock object every time, but is this possible/feasible with Zenject?

In theory, I should set up the new mock, add it to the DIContainer and re-inject my subject instance, right?

Mathijs-Bakker commented 5 years ago

Hi! It's been two months now that you've posted your question. And you probably have found your answer yet.

But YES! That's the great part with DI. As you are able to isolate your objects and write tests for them. You can bind a test double and inject it. You can also automate the process... https://github.com/svermeulen/Extenject/blob/master/Documentation/AutoMocking.md

PhilippCh commented 5 years ago

Thanks a lot, that's essentially what I did in the end :)