How to use it_should_throw_exception_from_nested_it_not_from_after_async method of NSpec.Tests.WhenRunningSpecs.Exceptions.AsyncAfterThrowsSpecClass class

Best NSpec code snippet using NSpec.Tests.WhenRunningSpecs.Exceptions.AsyncAfterThrowsSpecClass.it_should_throw_exception_from_nested_it_not_from_after_async

Run NSpec automation tests on LambdaTest cloud grid

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

when_async_after_contains_exception.cs

Source: when_async_after_contains_exception.cs Github

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