简体   繁体   中英

Should I test if my controller's Action method are calling the repository methods and generating the right view?

I'm new to testing and I just started testing my MVC application.

Currently I'm testing if my controller's action method are calling the right repository methods which in turn reads or writes the data from database.

What I'm also testing is if the return type of the action method is View , PartialView or RedirectToRoute , etc.

I've got some comments saying that testing if the controller's Action method is calling the right function in repository doesn't really make sense. Is it true?

What should I include in my Unit test for my MVC application, that uses Repository pattern as well.

It could make sense to check if you action call correct method on your repository but you'll need to mock it to avoid access to database. Unit tests should be isolated from external components.

Although it's not ideal, you could replace your "real" database by a lightweight in memory Sqlite to avoid mocking your database access in your tests.

I personally use Moq as mocking framework but it is plenty of mature mocking framework for .NET.

Take into account that testing if a method is called checks behavior instead of status. This make test more fragil as becomes dependent on internal implementation, but depending your scenario it could be perfeclty valid.

Unit testing is about testing a component's behavior in isolation , meaning that while testing a specific component, this component doesn't interract with any external component.

Usually, the way to do that is using mocks. All your dependencies must be mock so you can control them. Testing if a method have been called is valid. If the logic is not on your tested component, then your job is done. Your component call a function and case x,y and z in another case. Behavior is fine? Thats good enough.

If you have difficulties testing because you have a database dependency, thats usually a design problem. Usually, this is solved by using an database abstraction in front of the database, who its only job is the make call and return value from the database. That abstraction can be mock and injected in your tested class. That way, you can even return pre-configured values to your tested class and continue the process.

This depends on different scenarios, for ex, In Controller, You have one Action bool SaveEmployee() , which inside calls, service and then Database Layer to save. So testing whether Emp is actually saved in db does not make sense as it should be in another Unit Test for corresponding Database layer Function. Here, you just need to verify the status after it is successful, Failed, Duplicate or throws some Exception. You can simply Mock the function and return bool or string(like Success, as appropriate) and verify actual output with expected Output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM