public interface IFoo { void Bar(string parameter); } 在测试方法中,创建一个Moq对象,并通过.Object属性获取被模拟的实例。 代码语言:txt 复制 var fooMock = new Mock<IFoo>(); var foo = fooMock.Object; 设置模拟方法的行为,使用.Setup()方法来指定方法被调用时的期望行为。
设置out值不是问题,我不认为它提供了方法的“原始行为”……我认为你没有得到任何值,因为Setup没有找...
publicvoidShouldReturn_A_Dto_If_QueryBy_Id_With_Valid_Parameter() { varmoq =newMock<IDataBaseContext<MyDto>>; moq.Setup(a => a.GetElementById(It.IsAny<string>)).Returns(newMyDto); MyBll bll =newMyBll(moq.Object); vardto = bll.GetADto("afakeid"); dto.Should.NotBeNull; } 这里...
[Test]publicvoidShouldReturn_A_Dto_If_QueryBy_Id_With_Valid_Parameter(){varmoq =newMock<IDataBaseContext<MyDto>>(); moq.Setup(a => a.GetElementById(It.IsAny<string>())).Returns(newMyDto()); MyBll bll =newMyBll(moq.Object);vardto = bll.GetADto("afakeid"); dto.Should().NotB...
Parameter MatchingWhen putting together a Setup or Verify for a method, Moq requires the provision of all parameters that method may take, including those that are optional. This ensures that you set up the correct expectation, as, due to the overloading functionality within C#, it's possible...
Setup(foo => foo.DoSomething(It.IsAny<string>())).Returns(true); // any value passed in a `ref` parameter (requires Moq 4.8 or later): mock.Setup(foo => foo.Submit(ref It.Ref<Bar>.IsAny)).Returns(true); // matching Func<int>, lazy evaluated mock.Setup(foo => foo.Add(It....
[Test] public void ShouldReturn_A_Dto_If_QueryBy_Id_With_Valid_Parameter() { var moq = new Mock<IDataBaseContext<MyDto>>(); moq.Setup(a => a.GetElementById(It.IsAny<string>())).Returns(new MyDto()); MyBll bll = new MyBll(moq.Object); var dto = bll.GetADto("afakeid");...
The way how Moq is being used as a "distribution channel" for what (at this moment in time) essentially amounts to a social experiment strikes me as really unfair not just to Moq's user base, but (ironically) also to the project itself a...
So at the moment the only available options is to provider target option as a create mock parameter. class Origin { } const origin = new Origin(); const mock = new Mock<Origin>({target: new Origin()}); expect(typeof mock.object()).toBe(typeof new Origin()); in operator The ...
Execute BCP Out from C# executereader requires an open and available connection. the connection's current state is closed. ExecuteReader returns null with output parameter? Executing C# thread on specific interval of time Executing PowerShell command with parameters from C# Executing PsExec within C# ...