1

我最近开始使用 NSpec,现在我不确定如何扩展它。

重用规范 ( ) 的最佳方式是什么it["something"] = () => {};

假设我有一个接口IMyService和 2 个实现它的类:Service1Service2.

现在我想编写适用于IMyservice级别的规范,并针对我的 2 个实现类运行它们。

也许我在这里遗漏了一些东西,但我可以找到一种简单的方法来做到这一点。

4

1 回答 1

2

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

/*
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()
    {
    }
}

于 2012-08-11T14:22:29.507 回答