Advertisement
Guest User

GeneralThreadAffineContext with NUnit

a guest
Sep 28th, 2012
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.92 KB | None | 0 0
  1. using System.Threading.Tasks;
  2. using FluentAssertions;
  3. using NUnit.Framework;
  4.  
  5. // see http://nitoprograms.blogspot.ca/2012/02/async-unit-tests-part-2-right-way.html
  6. // include CapturerAndRestorer.cs and GeneralThreadAffineContext.cs from here:
  7. // http://www.symbolsource.org/Public/Metadata/NuGet/Project/AsyncUnitTests-MSTest/1.0.1/Release/.NETFramework,Version=v4.0,Profile=Client/Nito.AsyncEx.UnitTests.MSTest
  8. namespace ClassLibrary1
  9. {
  10.     public class Calculator
  11.     {
  12.         public Task<int> Add(int a, int b)
  13.         {
  14.             return Task.FromResult(42);
  15.         }
  16.     }
  17.  
  18.     [TestFixture]
  19.     public class Test
  20.     {
  21.         [Test]
  22.         public void ShouldAdd()
  23.         {
  24.             GeneralThreadAffineContext.Run(async () =>
  25.             {
  26.                 var c = new Calculator();
  27.                 var sum = await c.Add(1, 2);
  28.                 sum.Should().Be(3);
  29.             });
  30.         }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement