Best NSpec code snippet using NSpec.Tests.WhenRunningSpecs.BeforeAndAfter.before_all_sampleSpec.before_all
improperly_formed_context_methods.cs
Source:improperly_formed_context_methods.cs
...5{6 [TestFixture]7 public class improperly_formed_context_methods : when_running_specs8 {9 class before_all_sampleSpec : nspec10 {11 public static List<string> sequence = new List<string>();12 void a_regular_context_method()13 {14 //this context method wraps everything in lambdas (as context methods should),15 //because it's behavior is deferred using lambdas, it is run after the methods declared below.16 it["uses lambdas"] = () => sequence.Add("a_regular_context_method");17 }18 void messed_up_context()19 {20 //this contrived context method really should have everything wrapped in lambdas21 sequence.Add("messed_up_context");22 }23 void before_all()24 {25 //this is a properly crafted class level method, without lambda26 sequence.Add("before_all");27 }28 void another_messed_up_context()29 {30 //this context method should also have everything wrapped in lambdas31 //notice, it would sort alphabetically before the before_all method32 sequence.Add("another_messed_up_context");33 }34 }35 [Test]36 public void it_runs_things_in_a_strange_order()37 {38 before_all_sampleSpec.sequence = new List<string>();39 Run(typeof (before_all_sampleSpec));40 //the two improperly crafted context methods are executed first in the order they were declared41 //while nspec is building up its model of contexts and examples42 //then the class level before and properly crafted spec (wrapped in lambda) is executed.43 //The moral of the story is context methods that don't have their behavior wrapped44 //in lambdas (incorrectly so), run in the order that they are declared (disregarding alphabetical ordering).45 //FYI, alphabetical ordering can easily be implemented in the 'Methods' extension method.46 before_all_sampleSpec.sequence.Should().Equal(47 new[] { "messed_up_context", "another_messed_up_context", "before_all", "a_regular_context_method" });48 }49 }50}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!