I have a Blazor project that uses several services registered in program.cs.If I add any component to the project and then prepare tests for it in the Test project, it still requires that I register the services in the main project. For example, here is my program.cs:
builder.Services.AddScoped<DataService>();builder.Services.AddScoped<DataService2>();builder.Services.AddScoped<ITenantProvider, TenantProvider>();builder.Services.AddScoped<AppState>();builder.Services.AddSyncfusionBlazor();builder.Services.AddTransient<IEmailSender, EmailSender>();builder.Services.AddSingleton<ErrorLoggingService , ErrorLoggingService>();Now I add a simple component Test2.razor to the Pages folder:
<h3>Test2</h3>@code {}Now I setup a test in the Test project:
using Clinic.Pages;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Tests{ public class TestTests : TestContext { [Fact] public void Test1() { // Arrange var cut = RenderComponent<Test2>(); // Assert that content of the h3 match cut.Find("p").MarkupMatches("<h3>Test2</h3>"); } }}When I run the test, I get this error:
System.InvalidOperationException : Cannot provide a value for property 'DataService' on type 'Clinic.Pages.Test2'. There is no registered service of type 'Clinic.Models.DataService'.
Even though component Test2 does not use DataService or any other service. Why is that?