起订量和代码合同

发布于 2024-11-16 03:22:05 字数 449 浏览 2 评论 0原文

当使用类不变量时,代码契约似乎到处注入代码。像这样的事情

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ι不睡觉的鱼゛ 2024-11-23 03:22:05

该单元测试在运行时“通过”而不产生异常:

[ContractClassFor(typeof(IX))]
class IXContract
{
    [ContractInvariantMethod]
    void Invariant() { }
}

[ContractClass(typeof(IXContract))]
public interface IX { event EventHandler b; }

/// <summary>
/// Summary description for UnitTest1
/// </summary>
[TestClass]
public class UnitTest1
{
    public void MyTest()
    {
        var a = new Mock<IX>();

        a.Raise(x => x.b += null);
    }
}

我不完全确定发生了什么以及如何编译(或转录)上述内容,但我认为您不能使用“ContractClassFor”属性来装饰接口,并且您当然不能在接口中实现“{ ... }”。您还需要将您的接口 IX 公开以模拟它(或者使用属性中的 InternalsVisibleTo() 城堡代理进行内部操作)。

希望这会有所帮助,但如果这不能引导您实现您想要做的事情,请随时使用新代码更新您的帖子。

This unit test "passes" when run without generating exceptions:

[ContractClassFor(typeof(IX))]
class IXContract
{
    [ContractInvariantMethod]
    void Invariant() { }
}

[ContractClass(typeof(IXContract))]
public interface IX { event EventHandler b; }

/// <summary>
/// Summary description for UnitTest1
/// </summary>
[TestClass]
public class UnitTest1
{
    public void MyTest()
    {
        var a = new Mock<IX>();

        a.Raise(x => x.b += null);
    }
}

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.

灼痛 2024-11-23 03:22:05

您的代码不正确。它应该如下所示:

[ContractClassFor(typeof(IX))] 
internal abstract class XContract : IX
{   
    // Invariants belong on your actual implemenation; interfaces don't dictate implemenation so this isn't appropariate here.
    // [ ContractInvariantMethod] 
    // void Invariant() { ... } 

    public event EventHandler b { get { return default(EventHandler); } set { }}
} 

[ContractClass(typeof(XContract))] 
interface IX { event EventHandler b; } 

var a = new Mock<IX>(); 

a.Raise(x => x.b += null); 

请注意,Contract 类是内部抽象类,并且它现在也实现了您的接口。不变量在那里不合适,因为那是针对实现细节的;你不知道人们将如何实现你的界面,所以不要把它放在那里。不变方法的属性也是ContractInvariantMethodAttribute。

Your code is incorrect. It should look like this:

[ContractClassFor(typeof(IX))] 
internal abstract class XContract : IX
{   
    // Invariants belong on your actual implemenation; interfaces don't dictate implemenation so this isn't appropariate here.
    // [ ContractInvariantMethod] 
    // void Invariant() { ... } 

    public event EventHandler b { get { return default(EventHandler); } set { }}
} 

[ContractClass(typeof(XContract))] 
interface IX { event EventHandler b; } 

var a = new Mock<IX>(); 

a.Raise(x => x.b += null); 

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文