0

I've been struggling to perform unit test using xunit with my ASP .Net Core 2.1 MVC project. All answers i found are creating a solution, inside got classlibrary and xunit, then everything works perfectly fine. My case is i'm using MVC and i want to test my controllers, my controllers are my web service API, so i want to create a unit test using xunit. Due to all the answers in internet, does it means that using xunit for unit test for MVC project is not possible ?, if possible please share your answer. Thank you !.

2 Answers 2

2

Your question is a little hard to understand, but there's a few areas of confusion I think I can clear up.

First, xUnit is a test framework. It doesn't know or care anything about controllers, MVC, etc. It's all just code. In other words, yes, of course you can use xUnit to test controllers because one has nothing to do with the other.

Second, a unit test by definition should test one discreet unit of functionality. Controllers, however, involve a multitude of components: authentication and authorization, sessions, data access, view rendering, serialization, etc. As a result, a test involving a controller is by definition an integration test, not a unit test. The proper way to test a controller action is to utilize the test host.

Sign up to request clarification or add additional context in comments.

Comments

0

Especially with ASP.NET Core, which supports Dependency Injection out-of-the-box, you should think about pulling logic into separate classes and have your controllers be relatively stupid. This enables you to re-use the logic more easily. The classes containing the logic will be, if separated correctly, very easily testable. You're not responsible for testing the ASP.NET Core framework, so you shouldn't have to check to see if all the plumbing is done right. Only test your own logic.

Did you check Unit testing C# in .NET Core using dotnet test and xUnit and Getting started with xUnit.net (.NET Core / ASP.NET Core)?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.