重用NSpec规格规格、NSpec

2023-09-06 17:58:00 作者:未见佳人

我已经开始与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()
    {
    }
}