I am trying to write UI unit tests for my MVC core project and my problem is I am not able to find a MVC view generator or renderer that I can use in my unit test project.
I did implement a ViewRenderService for a specific requirement in another project but I cannot use it because of the dependencies cannot be injected into the unit test project.
What is a better way to approach this for dotnet core mvc? Any guidance will help.
Related
I have a solution in which only one class library project is there which has EF Core functionality. This project is being used only to deal with DB. API project is in different solution. Is there any way to unit test this project as just like unit testing of DB as stand alone instead of from API project.
I've been working in ASP.Net WebForms for the last 10 years and am trying to move myself into some more current technologies. I therefore am just doing a small Todo list project with the following outcomes.
Setup a central SQL DB
Create an ASP.Net Core Web API project to handle data requests
Have a ASP.Net Core MVC project that has a web interface
Create a PWA application to install on both iOS and Android
I have completed steps 1 and 2. I have got to step 3 and not sure how to configure the MVC app. I have a few questions around how I should set this up.
Please note that I have setup the Web API project with .Net Core 3.1 and have used Entity Framework and so therefore have a DB Context.
Should I house the MVC project in the same solution as the Web API project.
If I do house the MVC project in the same solution should I be calling the DBContext directly (c# not javascript) from the WebAPI (setup as a reference) or should I be calling the Web API controllers directly?
If I do house the MVC project and the web API project in the same solution - when I go to deploy this real world will I be able to separate the 2 projects as different hosts so that my WPA can get to the Web API.
Should I have the MVC project completetly seperated and consume the WebAPI project as an external project.
Thanks
2.
This can be organized in many ways. I can tell you how I would proceed.
(Assuming you are moving to .net core world).
I will try to describe how to organize your solution in separate projects, and I will assume you want to use entity-framework, repository and unit of work patterns for DB access.
I would create the following projects in my solution:
one .NET Standard project to hold only models (entities and DTO's) - name it "YourNamespace.Models"
one .NET Standard project to hold repository and unit of work contracts/interfaces - name it "YourNamespace.Contracts"
one .NET Standard project to be your data access layer - name it "YourNamespace.DAL". This is where you install EF Core, where you would have your entities configurations (if using Fluent API), your migrations would be here also (if needed), your repositories implementations, as well as the unit of work and database context.
one .NET Standard project for your services - name it "YourNamespace.Services"
one API project for your API controllers/REST services - name it "YourNamespace.TodoAPI"
one MVC project for MVC controllers - name it "YourNamespace.TodoMVC"
This structure is nice for using dependency injection for your services and anything else you need.
Make sure to select the Multiple Startup Projects option under Set StartUp Projects (right-click on your solution).
Select both API and MVC as startup projects.
Something similar is described here
I am now at the point where I want to test my application and found FluentDocker which allows me to run all my infrastructure as a xUnit test fixture. So far so good. The issue is that then I run not only my infrastructure, but also my application server as a docker container part of the docker-compose.
From my understanding this means I will not be able to use the TestServer facilities. Most of the examples I see seem to use the TestServer to shape the test environment. I actually do not want to shape the test environment, I want it to be as faithful to the deployment as possible, with configurations being managed by the docker-compose file.
Is my approach wrong? Am I buying a lot future effort in test setup? As my understanding on the abilities of the TestServer are the most lackluster, what is the advantage of using TestServer for ASP.net Core integration tests?
When integrating simple injector in to a pure .net core web api project, should I still use the "ASP.NET Core MVC Integration NuGet package"? I was expecting to find a separate integration package similar to older .net versions. Or is SimpleInjector.Integration.WebApi intended to be used for .net core as well?
With the introduction to ASP.NET Core, Microsoft created one unified model that integrates both MVC and Web API. They called this framework: ASP.NET Core MVC. This means that creating a Web API project, you use Microsoft.AspNetCore.Mvc.
This is why, when integrating Simple Injector with a ASP.NET Core MVC Web API project, you will have to use the SimpleInjector.Integration.AspNetCore.MVC or SimpleInjector.Integration.AspNetCore.MVC.Core package. Most likely, in your case, you should use the SimpleInjector.Integration.AspNetCore.MVC.Core, since it contains extension methods to register controllers, while SimpleInjector.Integration.AspNetCore.MVC builds on top of the Core package and adds MVC Razor Tag Helper integration, which is something you won't need when creating a Web API.
I have a bit of a problem and I have tried researching it everywhere but I cannot find an answer.
I going to be doing TDD development in Visual studio using MSTesting (Unit Testing .Net). However, I am going to developing a web application running Asp.Net Core 2.
This is where I am having a problem, The Unit testing project is running .NET Framework 4.7.1 meaning I can not reference the web application. I CAN NOT CHANGE THE FRAMEWORK OF THE WEB APPLICATION.
Please have a look at these screenshots:
I can not change the framework of the web application and the unit testing cant use Core 2. Please Help Thanks.
If you only have a .net core application or a .netstandard library I recommend you to create a MSTest Project that can run on .Net Core.