简体   繁体   中英

How do I include specs for a base class in the specs for an inherited class?

I have an RSSDataSourceParser class which is fully covered by MSpec specifications. I also have an inherited class, MultimediaRSSDataSourceParser . It does all of the parent functionality plus recognition of iTunes RSS extensions. I covered the extended functionality in separate specifications.

However, the business people who read the test suite only see the extended specifications for the MultimediaRSSDataSourceParser . They want to see the inherited functionality from RSSDataSourceParser , too.

Copying these specifications sounds absurd and would probably cause future code management problems. Is there any way to inherit the specifications from the parent?

MSpec has pretty useful behaviours that make reuse like this pretty easy. http://lostechies.com/jamesgregory/2010/01/18/behaviours-in-mspec/

I image the two types implement the same interface, or are at least inherited, when you say "it does all the parent [functionality]". You want to use MSpec's Behaves_like feature.

"Behaviours define reusable specs that encapsulate a particular set of, you guessed it, behaviours; you're then able to include these specs in any context that exhibits a particular behaviour." -- James Gregory

I don't know how an RSS data source reader works, so let me show you some example code where I used behaviors. I needed to convert an integer power of 2 (0, 1, 2, 4, etc) from some goofy API into an AZ character. So, the specifications would be

It should_convert_0_to_A = () => _converter.Convert(0).ShouldEqual('A');
It should_convert_1_to_B = () => _converter.Convert(1).ShouldEqual('B');
// ... and so on ...
It should_convert_16777216_to_Z = () => _converter.Convert(16777216).ShouldEqual('Z');

But, I had three different implementations of the interface. They included a binary conversion , a log conversion , and a lookup table .

public interface IUnitMaskConverter
{
    char Convert(uint mask);
}

I wasn't going to copy 26 specifications three or more times, So, I set the specs into a Behaviors class

[Behaviors]
public class UnitMaskConverterBehaviors
{
    It should_convert_0_to_A = () => _converter.Convert(0).ShouldEqual('A');
    It should_convert_1_to_B = () => _converter.Convert(1).ShouldEqual('B');
    // ... and so on ...
    It should_convert_16777216_to_Z = () => _converter.Convert(16777216).ShouldEqual('Z');

    protected static IUnitMaskConverter _converter;
}

So, then it's pretty easy to write a specs class that says this implementation behaves like a "unit mask converter"

[Subject(typeof(IUnitMaskConverter))]
public class When_converting_unit_masks_by_lookup
{
    Behaves_like<UnitMaskConverterBehaviors> a_unit_mask_converter;
    protected static LookupUnitMaskConverter _converter = new LookupUnitMaskConverter();
}

[Subject(typeof(IUnitMaskConverter))]
public class When_converting_unit_masks_by_log
{
    Behaves_like<UnitMaskConverterBehaviors> a_unit_mask_converter;
    protected static LogUnitMaskConverter _converter = new LogUnitMaskConverter();
}

[Subject(typeof(IUnitMaskConverter))]
public class When_converting_unit_masks_by_binary
{
    Behaves_like<UnitMaskConverterBehaviors> a_unit_mask_converter;
    protected static BinaryUnitMaskConverter _converter = new BinaryUnitMaskConverter();
}

The report even has all the behaviors listed per implementation.


IUnitMaskConverter specifications

4 contexts, 108 specifications

When converting unit masks by binary

26 specifications

  • should convert 0 to A
  • should convert 1 to B
  • ...
  • should convert 16777216 to Z

When converting unit masks by lookup

26 specifications

  • should convert 0 to A
  • should convert 1 to B
  • ...
  • should convert 16777216 to Z

This is not the case because testsuite for the RssDatasourceParser might be contained from large number of contexts. This means again lots of writing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM