Created
March 17, 2014 14:41
-
-
Save JustinJohnWilliams/9600483 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//here is a "familiar" test writen in NUnit. given that most developers | |
//are used to writing tests like this, | |
//you can see how different MSpec is | |
[TestFixture] | |
public class When_creating_a_new_account | |
{ | |
Account account = new Account(); | |
[Test] | |
public void Does_not_have_any_subscriptions() | |
{ | |
account.Subscriptions.Count.ShouldEqual(0); | |
} | |
[Test] | |
public void Does_not_have_any_sites() | |
{ | |
account.Subscriptions.Count.ShouldEqual(0); | |
} | |
[Test] | |
public void Generates_a_new_api_key() | |
{ | |
account.ApiKeys.First().ShouldNotBeEmpty(); | |
} | |
} | |
//NSpec tries not to "go against the grain", here is an NSpec test | |
//you can see it's exactly like the NUnit test, except instead of | |
//public access modifiers and attributes, you inherit from "nspec" | |
class When_creating_a_new_account : nspec | |
{ | |
Account account = new Account(); | |
void it_does_not_have_any_subscriptions() | |
{ | |
account.Subscriptions.Count.ShouldEqual(0); | |
} | |
void it_does_not_have_any_sites() | |
{ | |
account.Subscriptions.Count.ShouldEqual(0); | |
} | |
void it_generates_a_new_api_key() | |
{ | |
account.ApiKeys.First().ShouldNotBeEmpty(); | |
} | |
} | |
//okay...what's the point...sure it may be a *little* cleaner, but not *that* much cleaner | |
//well there are times where you want to "build up a context" using inheritance heirarchies | |
//there are A LOT of opinions on whether this is "right" or "wrong". If you feel it is wrong | |
//to use inheritance like this, then I don't think the rest of this gist will interest you. | |
//Though I'd still encourage you to read on. | |
//here is an NUnit sample using inheritance: | |
[TestFixture] | |
public class describe_NUnit | |
{ | |
[SetUp] | |
public void before_each() | |
{ | |
Console.WriteLine("I run before each test."); | |
} | |
[Test] | |
public void it_works_here() | |
{ | |
Assert.AreEqual("hello", "hello"); | |
} | |
} | |
[TestFixture] | |
public class category_of_examples : describe_NUnit | |
{ | |
[SetUp] | |
public void before_each_for_this_context() | |
{ | |
Console.WriteLine("I run before each test defined in this context."); | |
} | |
[Test] | |
public void it_also_works_here() | |
{ | |
Assert.AreEqual("hello", "hello"); | |
} | |
[Test] | |
public void it_works_here_too() | |
{ | |
Assert.AreEqual("hello", "hello"); | |
} | |
} | |
//you can do the same thing in NSpec. *but* NSpec gives you an alternative: | |
//the ability to use lambda expressions. This approach may or may not appeal to you. | |
//for a more comprehensive example, take a look at: https://gist.github.com/amirrajan/6701522 | |
class describe_NSpec : nspec | |
{ | |
void before_each() | |
{ | |
Console.WriteLine("I run before each test."); | |
} | |
void it_works_here() | |
{ | |
"hello".should_be("hello"); | |
} | |
void a_category_of_examples() | |
{ | |
before = () => Console.WriteLine("I run before each test defined in this context."); | |
it["also works here"] = () => "hello".should_be("hello"); | |
it["works here too"] = () => "hello".should_be("hello"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment