首页 > 解决方案 > 使用 Moq 测试采用结构的方法

问题描述

我有以下方法需要测试

public class S2SJwtGenerator : BaseJwtGenerator, IS2SJwtGenerator
{
    public S2SJwtGenerator();
    public string Generate(string tenantId, string username, TimeSpan lifetime);
}

我需要模拟生成。

protected readonly Mock<S2SJwtGenerator> tokenGen = new Mock<S2SJwtGenerator>();

var ts = new TimeSpan(1,1,1);

tokenGen.Setup(x => x.Generate(_testTenantId,null, ts)).Returns("NEWTOKEN");

我不断收到 notSupported 错误。

我尝试使用

tokenGen.Setup(x => x.Generate(_testTenantId,null, It.Ref<TimeSpan>.IsAny)).Returns("NEWTOKEN");

我仍然不受支持。TimeSpan是一个结构。我怎样才能让它工作

标签: c#unit-testingmoq

解决方案


推荐阅读