Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 测试方法的参数是否已更改_C#_Unit Testing_Moq_Xunit.net - Fatal编程技术网

C# 测试方法的参数是否已更改

C# 测试方法的参数是否已更改,c#,unit-testing,moq,xunit.net,C#,Unit Testing,Moq,Xunit.net,我正在尝试使用xUnit和Moq编写一些测试,但我似乎不知道如何测试提供给方法的参数是否已被方法本身更改 以下是我所拥有的: [事实] 创建AsyncCalledSomePropertySet()时公共无效 { //安排 var mockSomeService=new Mock(); var someObject=new someObject(); //表演 mockSomeService.Setup(x=>x.CreateAsync(someObject)).Callback(()=>{ so

我正在尝试使用xUnit和Moq编写一些测试,但我似乎不知道如何测试提供给方法的参数是否已被方法本身更改

以下是我所拥有的:

[事实]
创建AsyncCalledSomePropertySet()时公共无效
{
//安排
var mockSomeService=new Mock();
var someObject=new someObject();
//表演
mockSomeService.Setup(x=>x.CreateAsync(someObject)).Callback(()=>{
someObject.SomeProperty=“SomeValue”;
});
//断言
Assert.NotNull(someObject.SomeProperty);
}
基本上,我试图做的是确保调用
CreateAsync(someObject)
时,它设置
someObject
参数的
SomeProperty
属性。我的考试不及格

更新:

我正在尝试测试以下方法:

公共类SomeService:ISomeService
{
私有只读SomeContext\u context;
公共SomeService(SomeContext上下文)
{
_上下文=上下文;
}
公共异步任务CreateAsync(SomeObject SomeObject)
{
someObject.SomeProperty=GenerateRandomString();
_context.SomeObjects.Add(项目);
wait_context.SaveChangesAsync();
}
}

我想测试调用方法时是否设置了
SomeProperty
(如上所述)。如果我忘记设置它,我希望测试失败。

该服务与
SomeContext
实现问题紧密耦合。除非你计划在内存中进行测试,否则上下文将导致一些复杂的情况

抽象上下文

public interface ISomeContext {
    ISomeSet<SomeObject> SomeObjects { get; set; }
    Task<int> SaveChangesAsync(); 
}
测试将模拟被测试对象的依赖项的功能,并在执行测试时将其注入

public Task WhenCreateAsyncCalledSomePropertyIsSet() {
    // Arrange
    var mockSomeContext = new Mock<ISomeContext>();
    mockSomeContext
        .Setup(x => x.SaveChangesAsync())
        .ReturnsAsync(1); //Because the method under test is async

    var sut = new SomeService (mockSomeContext.Object); //Actual service, fake context
    var someObject = new SomeObject(); //actual object

    Assert.IsNull(someObject.SomeProperty); //making sure it was null to begin with

    // Act
    await sut.CreateAsync(someObject); //call method under test.
    var actual = someObject.SomeProperty; //get the value

    // Assert
    Assert.NotNull(actual); //Assert that it was actually set.
}
创建AsyncCalledSomePropertyIsSet()时的公共任务{ //安排 var mockSomeContext=new Mock(); 模拟上下文 .Setup(x=>x.saveChangesSync()) .ReturnsAsync(1);//因为被测试的方法是异步的 var sut=newsomeservice(mockSomeContext.Object);//实际服务,假上下文 var someObject=new someObject();//实际对象 Assert.IsNull(someObject.SomeProperty);//确保开始时为null //表演 wait sut.CreateAsync(someObject);//调用测试中的方法。 var-actual=someObject.SomeProperty;//获取值 //断言 Assert.NotNull(实际);//断言它实际上已设置。 } 您的真实上下文将从抽象/接口派生,或由提供该功能的东西包装

复习以更好地理解框架

public Task WhenCreateAsyncCalledSomePropertyIsSet() {
    // Arrange
    var mockSomeContext = new Mock<ISomeContext>();
    mockSomeContext
        .Setup(x => x.SaveChangesAsync())
        .ReturnsAsync(1); //Because the method under test is async

    var sut = new SomeService (mockSomeContext.Object); //Actual service, fake context
    var someObject = new SomeObject(); //actual object

    Assert.IsNull(someObject.SomeProperty); //making sure it was null to begin with

    // Act
    await sut.CreateAsync(someObject); //call method under test.
    var actual = someObject.SomeProperty; //get the value

    // Assert
    Assert.NotNull(actual); //Assert that it was actually set.
}