Skip to content

Instantly share code, notes, and snippets.

@ielcoro
Created April 13, 2013 14:07
Show Gist options
  • Save ielcoro/5378536 to your computer and use it in GitHub Desktop.
Save ielcoro/5378536 to your computer and use it in GitHub Desktop.
xUnit Post
[Fact]
public void GetAllShouldReturnResults()
{
}
public class ApiClientFixture : IDisposable
{
public ApiClientFixture(HttpServer server)
{
var uri = new Uri(Properties.Settings.Default.ApiClientUri);
this.Client = HttpClientFactory.Create(server);
}
public HttpClient Client { get; private set; }
#region Disposable
public bool Disposed { get; private set; }
public void Dispose()
{
if (this.Disposed) return;
//this.Client = null;
this.Disposed = true;
}
#endregion
}
public class ApiTests : IDisposable
{
private HttpClient client;
private string apiEndpoint;
//Initialize before every test execution
public ApiTest(string apiEndpoint)
{
this.apiEndpoint = apiEndpoint;
this.client = new HttpClient();
}
[Fact]
public void GetAllShouldReturnResults()
{
var response = this.client.GetStringAsync(this.apiEndpoint).Result;
var data = JObject.Parse<DataViewModel>(response);
Assert.True(data.Count() > 0);
}
//Clean after every test execution
public void Dispose()
{
if (this.client != null)
this.client = null;
}
}
public class ApiTestBase
: IUseFixture<ApiClientFixture>
{
public HttpClient Client { get; private set; }
//Runs before every test
public void SetFixture(ApiClientFixture data)
{
this.Client = data.Client
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment