How to use examples_body_should_still_run method of NSpec.Tests.WhenRunningSpecs.Exceptions.AfterThrowsSpecClass class

Best NSpec code snippet using NSpec.Tests.WhenRunningSpecs.Exceptions.AfterThrowsSpecClass.examples_body_should_still_run

Run NSpec automation tests on LambdaTest cloud grid

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

when_after_contains_exception.cs

Source: when_after_contains_exception.cs Github

copy
1using NSpec.Domain;
2using NUnit.Framework;
3using FluentAssertions;
4using System.Collections.Generic;
5using System.Linq;
6
7namespace NSpec.Tests.WhenRunningSpecs.Exceptions
8{
9    [TestFixture]
10    [Category("RunningSpecs")]
11    public class when_after_contains_exception : when_running_specs
12    {
13        class AfterThrowsSpecClass : nspec
14        {
15            void method_level_context()
16            {
17                after = () => { throw new AfterException(); };
18
19                it["should fail this example because of after"] = () =>
20                {
21                    ExamplesRun.Add("should fail this example because of after");
22                    Assert.That(true, Is.True);
23                };
24
25                it["should also fail this example because of after"] = () =>
26                {
27                    ExamplesRun.Add("should also fail this example because of after");
28                    Assert.That(true, Is.True);
29                };
30
31                it["preserves exception from same level it"] = () =>
32                {
33                    ExamplesRun.Add("preserves exception from same level it");
34                    throw new ItException();
35                };
36
37                context["exception thrown by both after and nested before"] = () =>
38                {
39                    before = () => { throw new BeforeException(); };
40
41                    it["preserves exception from nested before"] = () =>
42                    {
43                        ExamplesRun.Add("preserves exception from nested before");
44                        Assert.That(true, Is.True);
45                    };
46                };
47
48                context["exception thrown by both after and nested act"] = () =>
49                {
50                    act = () => { throw new ActException(); };
51
52                    it["preserves exception from nested act"] = () =>
53                    {
54                        ExamplesRun.Add("preserves exception from nested act");
55                        Assert.That(true, Is.True);
56                    };
57                };
58
59                context["exception thrown by both after and nested it"] = () =>
60                {
61                    it["preserves exception from nested it"] = () => 
62                    {
63                        ExamplesRun.Add("preserves exception from nested it");
64                        throw new ItException();
65                    };
66                };
67
68                context["exception thrown by both after and nested after"] = () =>
69                {
70                    it["preserves exception from nested after"] = () =>
71                    {
72                        ExamplesRun.Add("preserves exception from nested after");
73                        Assert.That(true, Is.True);
74                    };
75
76                    after = () => { throw new NestedAfterException(); };
77                };
78            }
79
80            public static List<string> ExamplesRun = new List<string>();
81        }
82
83        [SetUp]
84        public void setup()
85        {
86            Run(typeof(AfterThrowsSpecClass));
87        }
88
89        [Test]
90        public void the_example_level_failure_should_indicate_a_context_failure()
91        {
92            classContext.AllExamples()
93                .Where(e => !new []
94                {
95                    "preserves exception from same level it",
96                    "preserves exception from nested it",
97                }.Contains(e.Spec))
98                .Should().OnlyContain(e => e.Exception is ExampleFailureException);
99        }
100
101        [Test]
102        public void examples_with_only_after_failure_should_fail_because_of_after()
103        {
104            classContext.AllExamples()
105                .Where(e => new []
106                {
107                    "should fail this example because of after",
108                    "should also fail this example because of after",
109                }.Contains(e.Spec))
110                .Should().OnlyContain(e => e.Exception.InnerException is AfterException);
111        }
112
113        [Test]
114        public void it_should_throw_exception_from_same_level_it_not_from_after()
115        {
116            TheExample("preserves exception from same level it")
117                .Exception.Should().BeOfType<ItException>();
118        }
119
120        [Test]
121        public void it_should_throw_exception_from_nested_before_not_from_after()
122        {
123            TheExample("preserves exception from nested before")
124                .Exception.InnerException.Should().BeOfType<BeforeException>();
125        }
126
127        [Test]
128        public void it_should_throw_exception_from_nested_act_not_from_after()
129        {
130            TheExample("preserves exception from nested act")
131                .Exception.InnerException.Should().BeOfType<ActException>();
132        }
133
134        [Test]
135        public void it_should_throw_exception_from_nested_it_not_from_after()
136        {
137            TheExample("preserves exception from nested it")
138                .Exception.Should().BeOfType<ItException>();
139        }
140
141        [Test]
142        public void it_should_throw_exception_from_nested_after_not_from_after()
143        {
144            TheExample("preserves exception from nested after")
145                .Exception.InnerException.Should().BeOfType<NestedAfterException>();
146        }
147
148        [Test]
149        public void examples_should_fail_for_formatter()
150        {
151            formatter.WrittenExamples.Should().OnlyContain(e => e.Failed);
152        }
153
154        [Test]
155        public void examples_body_should_still_run()
156        {
157            string[] expecteds = new[]
158            {
159                "should fail this example because of after",
160                "should also fail this example because of after",
161                "preserves exception from same level it",
162                "preserves exception from nested act",
163                "preserves exception from nested it",
164                "preserves exception from nested after",
165            };
166
167            AfterThrowsSpecClass.ExamplesRun.ShouldBeEquivalentTo(expecteds);
168        }
169    }
170}
171
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
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)