我有一个 Blazor 页面,想要通过 bUnit 和 xUnit 进行单元测试。我想更改输入值并验证结果。
使用 InputText 一切正常。使用 InputNumber 我只能传递字符串。如果我通过数值保持不变。
我的 InputDate 绑定问题:我无法正确更改值。我试过了 :
cut.Find("#date input").Change(myDate.Date);
Run Code Online (Sandbox Code Playgroud)
该值保持不变(不变)。
cut.Find("#date input").Change(myDate.Date.ToString());
Run Code Online (Sandbox Code Playgroud)
或者
cut.Find("#date input").Change(myDate.Date.ToString("dd/MM/yyyy"));
Run Code Online (Sandbox Code Playgroud)
该值无效,验证消息:日期字段必须是日期。
我的 Blazor 页面:
cut.Find("#date input").Change(myDate.Date);
Run Code Online (Sandbox Code Playgroud)
和我的单元测试:
[Fact]
public void Test1()
{
DateTime myDate = new DateTime(2020, 11, 15, 15, 0, 0);
string myName = "bbb";
using var ctx = new TestContext();
// Act
var cut = ctx.RenderComponent<BlazorInputDate.Pages.Index>();
cut.Find("#name input").Change(myName);
cut.Find("#date input").Change(myDate.Date);
cut.Find("#num input").Change(myDate.Hour.ToString());
// Assert
Assert.Equal(myName, cut.Find("#name label").InnerHtml);
Assert.Equal(myDate.Hour.ToString(), cut.Find("#num label").InnerHtml);
Assert.Equal(myDate.ToString(), cut.Find("#date label").InnerHtml);
} …Run Code Online (Sandbox Code Playgroud) 从单元测试注入时出现此错误。
System.InvalidOperationException: ''NavigationManagerProxy' has not been initialized.'
Run Code Online (Sandbox Code Playgroud)
我的代码:
Services.AddSingleton<NavigationManager>(Mock.Of<NavigationManager>());
Run Code Online (Sandbox Code Playgroud) 有没有办法在 Blazor 服务器端项目中模拟 ProtectedSessionStorage?
我尝试了下面的代码,但收到错误:“要模拟的类型 (ProtectedSessionStorage) 必须是接口、委托或非密封、非静态类。”
private readonly Mock<ProtectedSessionStorage> _sessionStorage = new();
private readonly Mock<IDataProtector> _mockDataProtector = new();
private readonly Mock<IDataProtectionProvider> _mockDataProtectionProvider = new();
//in ctor()
Services.AddSingleton(_sessionStorage.Object);
//mock IDataProtector
_mockDataProtector = new Mock<IDataProtector>();
_mockDataProtector.Setup(sut => sut.Protect(It.IsAny<byte[]>())).Returns(Encoding.UTF8.GetBytes("protectedText"));
_mockDataProtector.Setup(sut => sut.Unprotect(It.IsAny<byte[]>())).Returns(Encoding.UTF8.GetBytes("originalText"));
Services.AddSingleton(_mockDataProtector.Object);
//mock IDataProtectionProvider
_mockDataProtectionProvider = new Mock<IDataProtectionProvider>();
_mockDataProtectionProvider.Setup(s => s.CreateProtector(It.IsAny<string>())).Returns(_mockDataProtector.Object);
Services.AddSingleton(_mockDataProtectionProvider.Object);
//in testMethod()
EquipmentSearchFilterDto filter = new();
filter.HospitalID = 1;
var result = new ProtectedBrowserStorageResult<EquipmentSearchFilterDto>();
_sessionStorage.Setup(x => x.GetAsync<EquipmentSearchFilterDto>(It.IsAny<string>()))
.ReturnsAsync(new ProtectedBrowserStorageResult<EquipmentSearchFilterDto>());
Run Code Online (Sandbox Code Playgroud)
我想过将 ProtectedSessionStorage 实现隐藏在接口后面,不幸的是我无法想出一个。有任何想法吗?
我收到此错误,为 Blazor 和 Blazorise 中的项目使用规范测试和 bunit 进行单元测试:
无法使用单例“Bunit.Rendering.WebTestRenderer”中的范围服务“Microsoft.AspNetCore.Components.IComponentActivator”
这是单元测试的代码:
using BlazorApp1.Pages;
using Blazorise;
using Blazorise.Bootstrap;
using Blazorise.Icons.FontAwesome;
using Blazorise.Modules;
using Bunit;
using Microsoft.AspNetCore.Components.RenderTree;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TechTalk.SpecFlow;
namespace TestProject1
{
[Binding]
public sealed class Feature1StepDefinitions
{
private IRenderedComponent<Home> _cut;
private readonly TestContext _ctx;
private readonly Mock<IClassProvider> _classProviderMock;
public Feature1StepDefinitions()
{
_ctx = new TestContext();
_ctx.Services.AddBlazorise(options =>
{
})
.AddBootstrapProviders()
.AddFontAwesomeIcons();
_ctx.Services.AddSingleton<IJSUtilitiesModule, JSUtilitiesModule>();
}
[Given("a list of stores")]
public …Run Code Online (Sandbox Code Playgroud)