重用NSpec规格 [英] Reusing NSpec specifications

查看:141
本文介绍了重用NSpec规格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始与NSpec最近,现在我不知道如何去衡量这一点。

I've started with NSpec recently and now I'm not sure how to scale this.

什么是重用规范的最好办法(吧[东西] =()=> {}; )?

What is the best way to reuse specifications (it["something"] = () => {};)?

比方说,我有一个接口 IMyService 和2类实现它:服务1 服务2

Let's say I have an interface IMyService and 2 classes that implement it: Service1 and Service2.

现在我想写的规格,在 IMyservice适用的水平,以及对我的2个实现类运行它们。

Now I want to write specifications that apply at IMyservice level, and run them against my 2 implementation classes.

也许我失去了一些东西,但我能找到一个简单的方法来做到这一点。

Maybe I'm missing something here, but I can find a simple way to do this.

推荐答案

您可以使用抽象类重用规范。下面是一个例子:

You can use a abstract class to reuse the specification. Here is an example:

/*
Output:

describe Service1
  it should do this
  it should also do this
  specify something unique to service1    
describe Service2
  it should do this
  it should also do this
  specify something unique to service2
*/


abstract class some_shared_spec : nspec
{
    public IMyservice service;

    void it_should_do_this()
    {

    }

    void it_should_also_do_this()
    {

    }
}

class describe_Service1 : some_shared_spec 
{
    void before_each()
    {
        service = new Service1();
    }

    void specify_something_unique_to_service1()
    {
    }
}

class describe_Service2 : some_shared_spec 
{
    void before_each()
    {
        service = new Service2();
    }

    void specify_something_unique_to_service2()
    {
    }
}

这篇关于重用NSpec规格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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