2012-06-03 26 views
126

ApiController'ım için bazı birim testleri yazmaya çalışıyorum ve bazı sorunlarla karşılaşıyorum. Yanıt oluşturma ile çok yardımcı olan Request.CreateResponse adlı güzel bir uzantı yöntemi vardır.ASP.NET WebApi birimi Request.CreateResponse ile test ediliyor

public HttpResponseMessage Post(Product product) 
{ 
    var createdProduct = repo.Add(product); 
    return this.Request.CreateResponse(HttpStatusCode.Created, createdProduct); 
} 

kısmi mocks kullanarak veya doğrudan "Yeni HttpResponseMessage (...)" nin kullanmadan CreateResponse alay için herhangi bir yolu var mı?

+1

Neden 'CreateResponse' alay etmek istiyorum? Neden doğru değerlerin ayarlandığı döndürülen 'HttpResponseMessage'' İçerik' ve 'StatusCode' özellikleri üzerinde durulmuyor? – nemesv

+3

Bu yöntemi form birimi sınamaları çalıştırırsam, yapılandırmanın ayarlanmadığı istisna ile başarısız olur. – asa

+1

Tamam, utanıyorum üzerimde, sadece bunu yazmam gerekiyordu.Request.CreateResponse (HttpStatusCode.Accepted, createdProduct, GlobalConfiguration.Configuration) – asa

cevap

215

Bunu çözmenin başka yolu da aşağıdaki yapmaktır:

controller.Request = new HttpRequestMessage(); 
controller.Request.SetConfiguration(new HttpConfiguration()); 

nedeni: Eğer 5.0 WebAPI yükseltme yapıyorsanız

controller.Request = new HttpRequestMessage(); 
controller.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, 
            new HttpConfiguration()); 

, o zaman bu değiştirmeniz gerekir Bunu yapmanız gerekir, çünkü denetleyicide Request var, aksi takdirde Request uzantıları çalışmayacak. Aksi takdirde, Yönlendirme aksi takdirde yönlendirme üzerinde bir HttpConfiguration olmalıdır ve boru hattının diğer bölümleri düzgün çalışmayacaktır.

+0

Evet, bu da işe yarayabilir, ancak Request.CreateResponse de çalışıyor gibi görünüyor. – asa

+4

GlobalConfiguration.Configuration eklendiğinde hala istek var null. İsteğin Ayarlanması benim için işe yaradı, ancak bu benim Eylemimin de Url.Link'i çağırdığı ve rota adının tanımlanmadığı gibi benim sorunumun sonu değildi. – foolshat

+0

Rotayı ayarlama hakkında yardım http://stackoverflow.com/questions/11779311/testing-webapi-controller-url-link – foolshat

20

Böyle test edilebilir kontrolör nesnesi kurmak olabilir:

var config = new HttpConfiguration(); 
var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/products"); 
var route = config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}"); 
var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "products" } }); 

controller.ControllerContext = new HttpControllerContext(config, routeData, request); 
controller.Request = request; 
controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config; 

Unit Testing ASP.NET Web API Peter Provost en kapsamlı blog yazısı kopyalandı. VB kullanarak benzer bir sorunu olan WebAPI 1 burada

0

.

Ben bunun bu kadar basit işe almak için buraya melez tepkiler başardı:

Dim request As HttpRequestMessage = New HttpRequestMessage() 
Return request.CreateResponse(HttpStatusCode.BadRequest, myCustomClassObject, GlobalConfiguration.Configuration) 

Sadece herkes yardımcı halinde gönderme.

0

Test sınıfınızda, denetleyici sınıfının bir örneğini oluşturun. örneğin var customerController= new CustomerController();

customerController.Request = new HttpRequestMessage(); 
customerController.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration()); 
İlgili konular