public async Task Timeout() { string json = "{\"Test:\"Success\"}"; var stopwatch = new Stopwatch(); await CTest <FakeServerContext> .Given(i => i.RegisterAUri(b => b.WithUri("http://fake.local/123") .WithResponse(json) .WithMethod(HttpMethod.Get) .WithBeforeReturn(() => Thread.Sleep(1000)) )) .WhenAsync(async i => { var client = i.Context.FakeServer.GetClient(); client.Timeout = TimeSpan.FromMilliseconds(100); try { await i.MakeTheRequest(client, "http://fake.local/123", HttpMethod.Get); } catch (Exception e) { i.Context.Exception = e; } }) .Then(t => Assert.True(t.Context.Exception is TaskCanceledException)) .ExecuteAsync(); }
public async Task Test1() { await CTest <TestContext> .Given(g => g.Context.FirstValue = 1) .And(g => g.AnotherValue(10)) .WhenAsync(async w => await w.IAddTheValues()) .AndAsync(async w => await w.IDivideBy(2)) .ThenAsync(async t => await t.TheAnswerIs(5.5d)) .ExecuteAsync(); }
public async Task EntityCreatesWithIdAndVersion() { await CTest <EntityContext <EntityClass> > .Given(a => a.EntityService()) .WhenAsync(i => i.CreateTheEntity()) .Then(t => t.VersionIs(1)) .And(t => t.IdIsGenerated()) .And(t => t.EventIsRaised($"{typeof(EntityClass).Name}_Created")) .ExecuteAsync(); }
public async Task Test1() { await CTest <TestContext> .Given(g => g.AValue()) .And(g => g.AnotherValue(10)) .When(w => w.IAddTheValues()) .And(w => w.IDivideBy(2)) .Then(t => t.TheAnswerIs(5.5d)) .And(t => t.True()) .ExecuteAsync(); }
public async Task NullBody() { await CTest <FakeServerContext> .Given(i => i.RegisterAUri(b => b.WithUri("http://fake.local/123") .WithResponse(null) .WithStatusCode(HttpStatusCode.Accepted))) .WhenAsync(i => i.MakeTheRequest("http://fake.local/123")) .ThenAsync(t => t.JsonIsReturned(null)) .And(t => t.StatusCodeIs(HttpStatusCode.Accepted)) .ExecuteAsync(); }
public async Task SimpleUri() { string json = "{\"Test:\"Success\"}"; await CTest <FakeServerContext> .Given(i => i.RegisterAUri(b => b.WithUri("http://fake.local/123") .WithResponse(json))) .WhenAsync(i => i.MakeTheRequest("http://fake.local/123")) .ThenAsync(t => t.JsonIsReturned(json)) .ExecuteAsync(); }
public async Task DisposeIsCalled() { bool disposed = false; Action <bool> setDisposed = d => disposed = d; await CTest <DisposableContext> .Given(q => q.Context.Disposer = setDisposed) .When(q => {}) .Then(q => {}) .ExecuteAsync(); Assert.True(disposed); }
public async Task ContentTypeHeader() { string json = "{\"Test:\"Success\"}"; await CTest <FakeServerContext> .Given(i => i.RegisterAUri(b => b.WithUri("http://fake.local/123") .WithResponse(json) .WithContentType("application/vnd.custom"))) .WhenAsync(i => i.MakeTheRequest("http://fake.local/123")) .ThenAsync(t => t.JsonIsReturned(json)) .And(t => t.ContentTypeIs("application/vnd.custom")) .ExecuteAsync(); }
public async Task CallingMethodOnEntityRaisesEvent() { await CTest <EntityContext <EntityClass> > .Given(a => a.EntityService()) .WhenAsync(i => i.CreateTheEntity()) .AndAsync(async i => await i.MethodIsCalledAsync(async e => await e.ArbitraryMethod())) .Then(t => t.EventIsRaised($"{typeof(EntityClass).Name}_Created")) .And(t => t.EventIsRaised($"{typeof(EntityClass).Name}_ArbitraryMethod")) .And(t => t.VersionIs(2)) .And(t => t.EventIdsMatch()) .ExecuteAsync(); }
public async Task LambdaMatcher() { string json = "{\"Test:\"Success\"}"; string body = "{\"MatchMe:\"123\"}"; await CTest <FakeServerContext> .Given(i => i.RegisterAUri(b => b.WithUri("http://fake.local/123") .WithResponse("{}"))) .And(i => i.RegisterAUri(b => b.WithUri("http://fake.local/123") .WithResponse(json) .WithMethod(HttpMethod.Post) .WithContentMatch(content => content?.Equals(body) ?? false) )) .WhenAsync(i => i.MakeTheRequest("http://fake.local/123", HttpMethod.Post, body)) .ThenAsync(t => t.JsonIsReturned(json)) .ExecuteAsync(); }
public async Task CallingMethodWithAnonymousObjectRaisesEvent() { await CTest <EntityContext <EntityClass> > .Given(a => a.EntityService()) .WhenAsync(i => i.CreateTheEntity()) .AndAsync(async i => await i.MethodIsCalledAsync(async e => await e.AnonymousMethod())) .Then(t => t.EventIsRaised($"{typeof(EntityClass).Name}_Created")) .And(t => t.EventIsRaised($"{typeof(EntityClass).Name}_AnonymousMethod", q => { dynamic data = q; Assert.True(data.Data1 == "This is my data"); Assert.True(data.Data2 == 6); })) .And(t => t.VersionIs(2)) .And(t => t.EventIdsMatch()) .ExecuteAsync(); }
public async Task PrereturnLambda() { string json = "{\"Test:\"Success\"}"; var stopwatch = new Stopwatch(); await CTest <FakeServerContext> .Given(i => i.RegisterAUri(b => b.WithUri("http://fake.local/123") .WithResponse(json) .WithMethod(HttpMethod.Get) .WithBeforeReturn(() => Thread.Sleep(1000)) )) .WhenAsync(i => { stopwatch.Start(); var value = i.MakeTheRequest("http://fake.local/123", HttpMethod.Get); stopwatch.Stop(); return(value); }) .ThenAsync(t => t.JsonIsReturned(json)) .And(c => Assert.True(stopwatch.ElapsedMilliseconds > 1000)) .ExecuteAsync(); }