Reusing NSpec specifications

2019-07-31 02:13发布

问题:

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"] = () => {};)?

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

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.

回答1:

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



标签: .net bdd nspec