起订量和代码合同
当使用类不变量时,代码契约似乎到处注入代码。像这样的事情
[ContractClassFor(typeof(IX))]
interface IXContract
{
[ClassInvariant]
void Invariant() { ... }
}
[ContractClass(typeof(IXContract))]
interface IX { event EventHandler b; }
var a = new Mock<IX>();
a.Raise(x => x.b += null);
失败并出现错误消息
Could not locate event for attach or detach method Void $InvariantMethod$().
有人知道解决方案吗?
When using class invariants, Code contracts seems to inject code everywhere. Stuff like this
[ContractClassFor(typeof(IX))]
interface IXContract
{
[ClassInvariant]
void Invariant() { ... }
}
[ContractClass(typeof(IXContract))]
interface IX { event EventHandler b; }
var a = new Mock<IX>();
a.Raise(x => x.b += null);
Fails with an error message
Could not locate event for attach or detach method Void $InvariantMethod$().
Anyone know of a solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该单元测试在运行时“通过”而不产生异常:
我不完全确定发生了什么以及如何编译(或转录)上述内容,但我认为您不能使用“ContractClassFor”属性来装饰接口,并且您当然不能在接口中实现“{ ... }”。您还需要将您的接口 IX 公开以模拟它(或者使用属性中的 InternalsVisibleTo() 城堡代理进行内部操作)。
希望这会有所帮助,但如果这不能引导您实现您想要做的事情,请随时使用新代码更新您的帖子。
This unit test "passes" when run without generating exceptions:
I'm not entirely sure what's going on and how you're compiling (or transcribing) the above, but I don't think you can have "ContractClassFor" attribute decorating an interface, and you certainly can't have the implementation "{ ... }" in an interface. You also need to make your interface IX public to mock it (or else internal with InternalsVisibleTo() castle proxy in your properties).
Hopefully this helps, but feel free to update your post with new code if this doesn't lead you toward what you're looking to do.
您的代码不正确。它应该如下所示:
请注意,Contract 类是内部抽象类,并且它现在也实现了您的接口。不变量在那里不合适,因为那是针对实现细节的;你不知道人们将如何实现你的界面,所以不要把它放在那里。不变方法的属性也是ContractInvariantMethodAttribute。
Your code is incorrect. It should look like this:
Notice that the Contract class is Internal Abstract, and that it also now implements your interface. The Invariant isn't appropiate there, as that's for implemenation details; you don't know how people will implemenent your interface, so don't put it there. Also the attribute for invariant methods is
ContractInvariantMethodAttribute
.