Ne test etmek istediğinize bağlıdır. Denetleyici uygulamalarınızı basitçe test etmek isterseniz, bir test projesi oluşturabilir ve projeye referans verebilir, kontrol cihazlarınızı manuel olarak başlatabilir ve testlerinizde bunları başlatabilirsiniz.
Ağ üzerinden entegrasyon testleri yapmak isterseniz, web api servisini kendi kendine barındırabilirsiniz. Ardından test projesine web api istemci paketini Install-Package Microsoft.AspNet.WebApi.Client
yükleyin ve istemci üzerinden çağırın.
bağlayan Örnek ve (bağlantısından) manuel örnekleme:
[TestClass]
public class TestSimpleProductController
{
[TestMethod]
public void GetAllProducts_ShouldReturnAllProducts()
{
var testProducts = GetTestProducts();
var controller = new SimpleProductController(testProducts);
var result = controller.GetAllProducts() as List<Product>;
Assert.AreEqual(testProducts.Count, result.Count);
}
Örnek (bağlantısından) web API istemcisi kullanarak:
http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api: Kendi adına
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:9000/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// New code:
HttpResponseMessage response = await client.GetAsync("api/products/1");
if (response.IsSuccessStatusCode)
{
Product product = await response.Content.ReadAsAsync>Product>();
Console.WriteLine("{0}\t${1}\t{2}", product.Name, product.Price, product.Category);
}
}
barındırma hizmeti
Web Api client için:
Install-Package Microsoft.AspNet.WebApi.Client
tüketmek için aşağıdaki kodu kullanın Web API projesi çalışıyor, bunun için Visual Studio'da MULTIPLE projesini aynı anda çalıştırmanız gerekiyor. –