我如何强制执行MSpec测试隔离时的静态成员被要求/方法? [英] How do I enforce test isolation in MSpec when static members/methods are required?

查看:366
本文介绍了我如何强制执行MSpec测试隔离时的静态成员被要求/方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定。我试图总结我的周围为什么MSpec采用静态方法/变量的头。 (当然不完全是静态方法,但成员变量的代表,它实际上是相同的)。



这使得它无法再利用上下文。这还是经过,并确保所有静态变量手动复位。这对测试隔离没有执行。如果一个测试设置一些变量并为它下一个检查,它会当它不应该通过。



这开始变得非常恼人。我要做一个什么,因为声明应该只是呆在那里,没有打通所有其他随机测试只是因为它是共享同一个范围内开展



编辑 -



现在的问题是,我该如何强制测试隔离。例如,看看下面的规格,共享 FooContext 。让我们来大胆猜测,如果 should_not_throw 通行证?



 公共类FooContext 
{
建立上下文=()=>主题=新的Foo();

公共静态美孚主题;
公共静态INT结果;
公共静态异常前;
}

公共类When_getting_an_int_incorrectly:FooContext
{
,因为=()=>将EX = Exception.Catch(()=>结果= Subject.GetInt(NULL));

它should_throw =()=> ex.ShouldNotBeNull();
}

公共类When_getting_an_int_correctly:FooContext
{
,因为=()=>将离= Exception.Catch(()=>结果= Subject.GetInt(0));

它should_not_throw =()=> ex.ShouldBeNull();
}


解决方案

这是一个技术和一个历史悠久限制。




  • 您需要静态字段共享代表之间的信息(建立,因为,它,清理)。

  • MSpec试图模仿rspec的,所以我觉得阿隆认为代表是一个不错的选择,并发布你今天看到的语法,早在2008年或2009年这种语法今天仍然存在。



至于上下文共享/背景的基类:从你说出它似乎像你过度使用的概念。你应该总是初始化在建立静态字段,所以它的全局状态将成为一个不是问题的问题。语境共享应该很好地考虑,所以,要你报价,它不会随机发生。尝试使用进行复杂的设置辅助方法,并更详细的(我会说显式)的组建。这将有助于使您的规格更具可读性。


Ok. I'm trying to wrap my head around why MSpec uses static methods / variables. (Well not exactly static methods, but with member variable delegates, it's practically the same).

This makes it impossible to reuse contexts. That or go through and make sure all static variables are reset manually. This has no enforcement on test isolation. If one test sets up some variables and the next one checks for it, it'd pass when it shouldn't.

This is starting to get very annoying. What I do in one "because" statement should just stay there, not get carried through to every other random test just because it's sharing the same context.

Edit-

The question is, how do I "ENFORCE" test isolation. For example, look at the specs below, sharing the FooContext. Let's take a wild guess if should_not_throw passes?

public class FooContext
{
    Establish context = () => Subject = new Foo();

    public static Foo Subject;
    public static int result;
    public static Exception ex;
}

public class When_getting_an_int_incorrectly : FooContext
{
    Because of = () => ex = Exception.Catch(() => result = Subject.GetInt(null)); 

    It should_throw = () => ex.ShouldNotBeNull();
}

public class When_getting_an_int_correctly : FooContext
{
    Because of = () => ex = Exception.Catch(() => result = Subject.GetInt(0));

    It should_not_throw = () => ex.ShouldBeNull();
}

解决方案

It's a technical and a historic limitation.

  • You need statics fields to share information between the delegates (Establish, Because, It, Cleanup).
  • MSpec tries to mimic rspec, so I think Aaron considered delegates to be a good fit and released the syntax you see today back in 2008 or 2009. This syntax is still in place today.

As for context sharing / context base classes: From what you state it seems like you're overusing the concept. You should always initialize static fields in the Establish, so it the global state will become a non-issue. Context sharing should be well considered, so, to quote you, it doesn't happen randomly. Try using helper methods for complex setup and be more verbose (I'd say explicit) in the Establishs. It will help make your specs more readable.

这篇关于我如何强制执行MSpec测试隔离时的静态成员被要求/方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆