1

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.

Amir
  • 9,091
  • 5
  • 34
  • 46
Pedro
  • 1,134
  • 11
  • 26

1 Answers1

2

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

Amir
  • 9,091
  • 5
  • 34
  • 46
  • Thanks for that Amir. It's been a long time since I looked into this, but I was under the impression that methods in base classes were not be included. I'll give it a go again. Thanks! – Pedro Aug 12 '12 at 00:05
  • Feel free to reach out to me anytime on twitter or by email, if you need some good examples to NSpec tests I would recommend this: http://stackoverflow.com/questions/10741104/can-anyone-show-me-some-examples-of-nspec-being-used-to-test-controllers-and-ot – Amir Aug 13 '12 at 19:46
  • This was added in a later version of NSpec, be sure to use the latest version. – Amir Aug 14 '12 at 03:13