本文介绍了最小起订量如何正确模拟仅设置属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是正确的方式来处理接口设置曝光仅物业,起订量? previously我已经添加了其他的访问,但这已经耗尽了我的领域太远随机整个抛出新的NotImplementedException()
语句。
我只想做一些简单的像:
mock.VerifySet(查看=> view.SetOnlyValue,Times.Never());
不过,这会产生一个编译错误属性SetOnlyValue没有吸气
解决方案
公共类XYZ
{
公共虚拟字符串AA {集合{}}
}
公共类VerifySyntax
{
[事实]
公共无效ThisIsHow()
{
VAR XYZ =新的模拟< XYZ>();
xyz.Object.AA =BB;
//抛出:
xyz.VerifySet(S => s.AA = It.IsAny&其中;串GT;(),Times.Never());
}
}
公共类SetupSyntax
{
[事实]
公共无效ThisIsHow()
{
VAR XYZ =新的模拟< XYZ>();
xyz.SetupSet(S => s.AA = It.IsAny&其中;串GT;()).Throws(新出现InvalidOperationException());
Assert.Throws<出现InvalidOperationException>(()=> xyz.Object.AA =BB);
}
}
What is the correct way for dealing with interfaces the expose set-only properties with Moq? Previously I've added the other accessor but this has bled into my domain too far with random throw new NotImplementedException()
statements throughout.
I just want to do something simple like:
mock.VerifySet(view => view.SetOnlyValue, Times.Never());
But this yields a compile error of The property 'SetOnlyValue' has no getter
解决方案
public class Xyz
{
public virtual string AA { set{} }
}
public class VerifySyntax
{
[Fact]
public void ThisIsHow()
{
var xyz = new Mock<Xyz>();
xyz.Object.AA = "bb";
// Throws:
xyz.VerifySet( s => s.AA = It.IsAny<string>(), Times.Never() );
}
}
public class SetupSyntax
{
[Fact]
public void ThisIsHow()
{
var xyz = new Mock<Xyz>();
xyz.SetupSet( s => s.AA = It.IsAny<string>() ).Throws( new InvalidOperationException( ) );
Assert.Throws<InvalidOperationException>( () => xyz.Object.AA = "bb" );
}
}
这篇关于最小起订量如何正确模拟仅设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!