C# Moq无法在单元测试中引发异常

C# Moq无法在单元测试中引发异常,c#,.net,moq,C#,.net,Moq,我有一个Azure函数,它将应用程序设置存储在Azure Blob存储中。为了对获取并添加设置的类进行单元测试,我使用moq让blob存储抽象类(blobStorageRepository)抛出异常。它主要起作用。然而,我有两个测试失败 我还有其他模拟blobStorageRepository的单元测试。所有工作都很好,包括针对“Get”方法的测试都正确地抛出异常,但“Add”异常测试失败。我已经包括了下面的实际测试 Fact(DisplayName = "AddUserSettings

我有一个Azure函数,它将应用程序设置存储在Azure Blob存储中。为了对获取并添加设置的类进行单元测试,我使用moq让blob存储抽象类(blobStorageRepository)抛出异常。它主要起作用。然而,我有两个测试失败

我还有其他模拟blobStorageRepository的单元测试。所有工作都很好,包括针对“Get”方法的测试都正确地抛出异常,但“Add”异常测试失败。我已经包括了下面的实际测试

    Fact(DisplayName = "AddUserSettingsAsync - InvalidOperationException")]
    [Trait("Category", "Unit Test")]
    public async Task SettingsStoreAddUserSettingsTestWithException()
    {
        string userObject = Guid.NewGuid().ToString();
        string correlationId = Guid.NewGuid().ToString();

        string body = File.ReadAllText("TestData/userSettings.json");
        UserSettingsObject userSettingsObject = JsonConvert.DeserializeObject<UserSettingsObject>(body);

        var iFunctionEnvironment = TestHelpers.GetEnvironmentVariable("Test");

        Uri.TryCreate("http://localhost", UriKind.Absolute, out Uri uri);

        var iblobStorageRepositoryMoq = new Mock<IBlobStorageRepository>();
        iblobStorageRepositoryMoq
            .Setup(mock => mock.Add(logger, correlationId, body, userObject))
            .ThrowsAsync(new Exception("Function Add threw an exception"));

        var iblobStorageRepository = iblobStorageRepositoryMoq.Object;

        SettingsStore settingsStore = new SettingsStore(iFunctionEnvironment, iblobStorageRepository);

        Exception exception = await Assert.ThrowsAsync<InvalidOperationException>(async () => await settingsStore.AddUserSettingsAsync(logger, correlationId, userSettingsObject, userObject));
        Assert.Equal("Function Add threw an exception", exception.Message);
        Assert.Null(exception.InnerException);
    }
Fact(DisplayName=“AddUserSettingsAsync-InvalidOperationException”)]
[特征(“类别”、“单元测试”)]
公共异步任务设置StoreAddUserSettingStestWithException()存储添加用户设置
{
字符串userObject=Guid.NewGuid().ToString();
字符串correlationId=Guid.NewGuid().ToString();
string body=File.ReadAllText(“TestData/userSettings.json”);
UserSettingsObject UserSettingsObject=JsonConvert.DeserializeObject(正文);
var iFunctionEnvironment=TestHelpers.GetEnvironmentVariable(“Test”);
Uri.TryCreate(“http://localhost“,UriKind.Absolute,out Uri);
var iblobStorageRepositoryMoq=new Mock();
IBlobstoragerpositoryMOQ
.Setup(mock=>mock.Add(logger、correlationId、body、userObject))
.ThrowsAsync(新异常(“函数添加引发异常”));
var iblobstoragepository=iblobstoragepositorymoq.Object;
SettingsStore SettingsStore=新的SettingsStore(iFunctionEnvironment,iblobStorageRepository);
Exception Exception=await Assert.ThrowsAsync(async()=>await settingsStore.AddUserSettingsAsync(logger,correlationId,userSettingsObject,userObject));
Assert.Equal(“函数Add引发异常”,exception.Message);
Assert.Null(exception.InnerException);
}
以下是blogStoreRepository的界面:

任务添加(ILogger记录器、字符串关联ID、字符串设置对象、字符串设置对象名称)


任何帮助都将不胜感激

如果调用的mock的行为不符合预期,大多数情况下是因为安装程序与实际调用的不匹配

考虑使用
It.IsAny()


测试中的方法是什么样子的。模拟预期很可能与实际调用的不匹配。
logger
来自何处?很好。logger是azure函数自身提供的.net logger。将在上面添加测试中的函数以供记录。你的建议奏效了!谢谢几天来我一直在用头撞墙。。。。
Fact(DisplayName = "AddUserSettingsAsync - InvalidOperationException")]
[Trait("Category", "Unit Test")]
public async Task SettingsStoreAddUserSettingsTestWithException() {
    //Arrange
    string userObject = Guid.NewGuid().ToString();
    string correlationId = Guid.NewGuid().ToString();

    string body = File.ReadAllText("TestData/userSettings.json");
    UserSettingsObject userSettingsObject = JsonConvert.DeserializeObject<UserSettingsObject>(body);

    var iFunctionEnvironment = TestHelpers.GetEnvironmentVariable("Test");

    Uri.TryCreate("http://localhost", UriKind.Absolute, out Uri uri);

    var iblobStorageRepositoryMoq = new Mock<IBlobStorageRepository>();
    iblobStorageRepositoryMoq
        .Setup(mock => mock.Add(It.IsAny<ILogger>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
        .ThrowsAsync(new InvalidOperationException("Function Add threw an exception"));

    //The SUT
    var subjectUnderTest = new SettingsStore(iFunctionEnvironment, iblobStorageRepositoryMoq.Object);

    //Act
    InvalidOperationException exception = await Assert.ThrowsAsync<InvalidOperationException>(() => subjectUnderTest.AddUserSettingsAsync(logger, correlationId, userSettingsObject, userObject));

    //Assert
    Assert.Equal("Function Add threw an exception", exception.Message);
    Assert.Null(exception.InnerException);
}
//...

var iblobStorageRepositoryMoq = new Mock<IBlobStorageRepository>();
iblobStorageRepositoryMoq
    .Setup(mock => mock.Add(It.IsAny<ILogger>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
    .ThrowsAsync(new InvalidOperationException("Function Add threw an exception"));

//...