Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSpec, what should I put in my [Subject()] attributes?

Tags:

tdd

bdd

mspec

I've been using MSpec for a little while and I really like it. I've found that to get ReSharper to recognize my specifications, I need to use a SubjectAttribute.

I'm wondering though, what's the best thing to put in the [Subject()] attributes?

If I'm doing BDD, then I don't know the type under test so [Subject(typeof(thingy))] seems premature. Could be added later I suppose once the code is written.

So that leaves the text version, [Subject("some text")]. But what's the best thing to put there?

Whatever I do, it doesn't seem to affect the output I get in ReSharper. I suppose to some extent this is down to personal preference, but I wondered if there was any convention here?

like image 926
Tim Long Avatar asked Aug 20 '11 23:08

Tim Long


1 Answers

You don't need to apply SubjectAttribute to have ReSharper recognize contexts and specifications, a class containing an It field will suffice. However, if you want ReSharper to support custom naming conventions for MSpec types and fields (Because et al) you need to apply the SubjectAttribute:

  1. Define custom naming conventions in ReSharper | Options | Languages/Common section | Naming Style | Advanced Settings

    When you add a User defined naming rule scroll down the list to see MSpec entities.

  2. Enable MSpec annotations in ReSharper | Options | Code Inspection/Code Annotations section

    Annotations + SubjectAttribute (even without custom naming rules) prevent ReSharper from marking MSpec's fields as unused. Alternatively, disable warning 169 in the project settings.

Subject serves as metadata describing you context, for example you can use the System Under Test (when writing a unit test), a string of your choice, or both. These information will be reported in the HTML and in the ReSharper output. It doesn't work as of now, I suspect this is a bug in the ReSharper runner for 6.0.

As with strings, you can basically put anything you want there. I would recommend using the subject to group your specs by feature.

Subject: Login

Contexts: When logging in with valid credentials, When logging in with invalid credentials, etc.

There's an example in my GitHub repository.

like image 96
Alexander Groß Avatar answered Oct 14 '22 23:10

Alexander Groß