How to use grand_child_spec class of NSpec.Tests package

Best NSpec code snippet using NSpec.Tests.grand_child_spec

Run NSpec automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

describe_ContextBuilder.cs

Source: describe_ContextBuilder.cs Github

copy
1using FluentAssertions;
2using Moq;
3using NSpec.Domain;
4using NUnit.Framework;
5using System;
6using System.Collections.Generic;
7using System.Linq;
8using System.Threading.Tasks;
9
10namespace NSpec.Tests
11{
12    [TestFixture]
13    public class describe_ContextBuilder
14    {
15        protected Mock<ISpecFinder> finder;
16
17        protected ContextBuilder builder;
18
19        protected List<Type> typesForFinder;
20
21        protected List<Context> contexts;
22
23        [SetUp]
24        public void setup_base()
25        {
26            finder = new Mock<ISpecFinder>();
27
28            typesForFinder = new List<Type>();
29
30            finder.Setup(f => f.SpecClasses()).Returns(typesForFinder);
31
32            DefaultConventions conventions = new DefaultConventions();
33
34            conventions.Initialize();
35
36            builder = new ContextBuilder(finder.Object, conventions);
37        }
38
39        public void GivenTypes(params Type[] types)
40        {
41            typesForFinder.AddRange(types);
42        }
43
44        public IList<Context> TheContexts()
45        {
46            return builder.Contexts();
47        }
48    }
49
50    [TestFixture]
51    public class when_building_contexts : describe_ContextBuilder
52    {
53        public class child : parent { }
54
55        public class sibling : parent { }
56
57        public class parent : nspec { }
58
59        [SetUp]
60        public void setup()
61        {
62            GivenTypes(typeof(child), typeof(sibling), typeof(parent));
63
64            TheContexts();
65        }
66
67        [Test]
68        public void should_get_specs_from_specFinder()
69        {
70            finder.Verify(f => f.SpecClasses());
71        }
72
73        [Test]
74        public void the_primary_context_should_be_parent()
75        {
76            TheContexts().First().ShouldBeNamedAfter(typeof(parent));
77        }
78
79        [Test]
80        public void the_parent_should_have_the_child_context()
81        {
82            TheContexts().First().Contexts.First().ShouldBeNamedAfter(typeof(child));
83        }
84
85        [Test]
86        public void it_should_only_have_the_parent_once()
87        {
88            TheContexts().Count().Should().Be(1);
89        }
90
91        [Test]
92        public void it_should_have_the_sibling()
93        {
94            TheContexts().First().Contexts.Should().Contain(c => c.Name == typeof(sibling).Name);
95        }
96
97    }
98
99    [TestFixture]
100    public class when_finding_method_level_examples : describe_ContextBuilder
101    {
102        class class_with_method_level_example : nspec
103        {
104            void it_should_be_considered_an_example() { }
105
106            void specify_should_be_considered_as_an_example() { }
107
108            // -----
109
110            async Task it_should_be_considered_an_example_with_async() { await Task.Delay(0); }
111
112            async Task<long> it_should_be_considered_an_example_with_async_result() { await Task.Delay(0); return 0L; }
113
114            async void it_should_be_considered_an_example_with_async_void() { await Task.Delay(0); }
115
116            async Task specify_should_be_considered_as_an_example_with_async() { await Task.Delay(0); }
117        }
118
119        [SetUp]
120        public void setup()
121        {
122            GivenTypes(typeof(class_with_method_level_example));
123        }
124
125        [Test]
126        public void should_find_method_level_example_if_the_method_name_starts_with_the_word_IT()
127        {
128            ShouldContainExample("it should be considered an example");
129        }
130
131        [Test]
132        public void should_find_async_method_level_example_if_the_method_name_starts_with_the_word_IT()
133        {
134            ShouldContainExample("it should be considered an example with async");
135        }
136
137        [Test]
138        public void should_find_async_method_level_example_if_the_method_name_starts_with_the_word_IT_and_it_returns_result()
139        {
140            ShouldContainExample("it should be considered an example with async result");
141        }
142
143        [Test]
144        public void should_find_async_method_level_example_if_the_method_name_starts_with_the_word_IT_and_it_returns_void()
145        {
146            ShouldContainExample("it should be considered an example with async void");
147        }
148
149        [Test]
150        public void should_find_method_level_example_if_the_method_starts_with_SPECIFY()
151        {
152            ShouldContainExample("specify should be considered as an example");
153        }
154
155        [Test]
156        public void should_find_async_method_level_example_if_the_method_starts_with_SPECIFY()
157        {
158            ShouldContainExample("specify should be considered as an example with async");
159        }
160
161        [Test]
162        public void should_exclude_methods_that_start_with_ITs_from_child_context()
163        {
164            TheContexts().First().Contexts.Count.Should().Be(0);
165        }
166
167        private void ShouldContainExample(string exampleName)
168        {
169            TheContexts().First().Examples.Any(s => s.Spec == exampleName);
170        }
171    }
172
173    [TestFixture]
174    public class when_building_method_contexts
175    {
176        private Context classContext;
177
178        private class SpecClass : nspec
179        {
180            public void public_method() { }
181
182            void private_method() { }
183
184            void before_each() { }
185
186            void act_each() { }
187        }
188
189        [SetUp]
190        public void setup()
191        {
192            var finder = new Mock<ISpecFinder>();
193
194            DefaultConventions defaultConvention = new DefaultConventions();
195
196            defaultConvention.Initialize();
197
198            var builder = new ContextBuilder(finder.Object, defaultConvention);
199
200            classContext = new Context("class");
201
202            builder.BuildMethodContexts(classContext, typeof(SpecClass));
203        }
204
205        [Test]
206        public void it_should_add_the_public_method_as_a_sub_context()
207        {
208            classContext.Contexts.Should().Contain(c => c.Name == "public method");
209        }
210
211        [Test]
212        public void it_should_not_create_a_sub_context_for_the_private_method()
213        {
214            classContext.Contexts.Should().Contain(c => c.Name == "private method");
215        }
216
217        [Test]
218        public void it_should_disregard_method_called_before_each()
219        {
220            classContext.Contexts.Should().NotContain(c => c.Name == "before each");
221        }
222
223        [Test]
224        public void it_should_disregard_method_called_act_each()
225        {
226            classContext.Contexts.Should().NotContain(c => c.Name == "act each");
227        }
228    }
229
230    [TestFixture]
231    public class when_building_class_and_method_contexts_with_tag_attributes : describe_ContextBuilder
232    {
233        [Tag("@class-tag")]
234        class SpecClass : nspec
235        {
236            [Tag("@method-tag")]
237            void public_method() { }
238        }
239
240        [SetUp]
241        public void setup()
242        {
243            GivenTypes(typeof(SpecClass));
244        }
245
246        [Test]
247        public void it_should_tag_class_context()
248        {
249            var classContext = TheContexts()[0];
250            classContext.Tags.Should().Contain("@class-tag");
251        }
252
253        [Test]
254        public void it_should_tag_method_context()
255        {
256            var methodContext = TheContexts()[0].Contexts[0];
257            methodContext.Tags.Should().Contain("@method-tag");
258        }
259    }
260
261    [TestFixture]
262    [Category("ContextBuilder")]
263    public class describe_second_order_inheritance : describe_ContextBuilder
264    {
265        class base_spec : nspec { }
266
267        class child_spec : base_spec { }
268
269        class grand_child_spec : child_spec { }
270
271        [SetUp]
272        public void setup()
273        {
274            GivenTypes(typeof(base_spec),
275                typeof(child_spec),
276                typeof(grand_child_spec));
277        }
278
279        [Test]
280        public void the_root_context_should_be_base_spec()
281        {
282            TheContexts().First().ShouldBeNamedAfter(typeof(base_spec));
283        }
284
285        [Test]
286        public void the_next_context_should_be_derived_spec()
287        {
288            TheContexts().First().Contexts.First().ShouldBeNamedAfter(typeof(child_spec));
289        }
290
291        [Test]
292        public void the_next_next_context_should_be_derived_spec()
293        {
294            TheContexts().First().Contexts.First().Contexts.First().ShouldBeNamedAfter(typeof(grand_child_spec));
295        }
296    }
297
298    public static class InheritanceExtentions
299    {
300        public static void ShouldBeNamedAfter(this Context context, Type expectedType)
301        {
302            string actual = context.Name;
303            string expected = expectedType.Name.Replace("_", " ");
304
305            actual.Should().Be(expected);
306        }
307    }
308}
309
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)