What should be the strategy of unit testing when using IoC?

后端 未结 4 1753
说谎
说谎 2021-02-12 23:29

After all what I have read about Dependency Injection and IoC I have decided to try to use Windsor Container within our application (it\'s a 50K LOC multi-layer web app, so I ho

4条回答
  •  北荒
    北荒 (楼主)
    2021-02-13 00:04

    I'm working on an ASP.NET MVC project with about 400 unit tests. I am using Ninject for dependency injection and MBUnit for testing.

    Ninject is not really necessary for unit testing, but it reduces the amount of code I have to type. I only have to specify once (per project) how my interfaces should be instantiated as opposed to doing this every time I initialize the class being tested.

    In order to save time on writing new tests, I have created test fixture base classes with as much generic setup code as possible. The setup procedures in those classes intialize fake repositories, create some test data and a fake identity for the test user. Unit tests only initialize data that is too specific to go into generic setup procedures.

    I am also mocking objects (as opposed to faking) in some tests, but I found that faking data repositories results in less work and more accurate tests.

    For example, it would be more difficult to check if the method under test I properly commits all updates to the repository after making them when using a repository mock than when I am using a repository fake.

    It was quite a bit of work to set up at the beginning, but really helped me reduce save a lot of time in the long run.

提交回复
热议问题