2016-06-13 58 views
0

我正在重構我的測試工具(SpecFlow 2.0.0 over NUnit 3.0.5797.27534)以驅動我的控制器進行特定測試。在IHttpControllerActivator中嘲弄Do-Not-Cares

Scenario: FluxCapacitor Works 
    Given I have entered '11-05-1955' into the Time Circuits 
    When I get the DeLorean up to 88 mph 
    Then the date on the newspaper should be '11-05-1955' 

這是一個常見的腳手架示例,我跑我的測試架構。

public class TimeController: ApiController 
{ 
    private TimeProvider TimeProvider{get;set;} 
    public TimeController(TimeProvider timeProvider) 
    { 
     this.TimeProvider = timeProvider; 
    } 
} 
[HttpGet] 
[Route("Time;now")] 
public DateTime GetCurrentTime() 
{ 
    return TimeProvider.Current.Now; 
} 

...所以,我試圖模擬出的Then調用來完成...

[Then(@"the date on the newspaper should be '(.*)'")] 
public void ThenTheDateOnTheNewspaperShouldBe(string p0) 
{ 
    DateTime expected = DateTime.Parse(p0); 
    Startup startup = new Startup(); 

    Hosting.MySystemStartupOptions options = new Hosting.MySystemStartupOptions(); 


    //+===================================================================+ 
    //[KAB] This is the line that I think is of primary importance in this 
    options.Dispatcher = DelegateThatReturnsMyMockControllerActivator(); 
    //+===================================================================+ 
    startup.Register =() => { return options;}; 

    //+===================================================================+ 
    //[options.Dispatcher] gets pushed down to my WebApiConfiguration that 
    //executes [Services.Replace(typeof(IHttpControllerActivator),options.Dispatcher] 
    //+===================================================================+ 
    using(WebApp.Start(url: "http://localhost:9000/", startup:startup.Configuration)) 
    using(var client = new System.Net.Http.HttpClient()) 
    using(System.Net.Http.HttpResponseMessage response = client.GetAsync(ResourceUnderTest.ToString()).Result) 
    { 
     if(response.StatusCode != System.Net.HttpStatusCode.OK) 
     { 
     Assert.Fail("you suck"); 
     } 

     //never getting past, because I suck. 
    } 
} 

使返回我的IHttpControllerActivator實際上是所謂Composer

作曲委託返回一個模擬。

Composer看起來正是如此:

Composer =() => 
{ 
    var request = new Moq.Mock<System.Net.Http.HttpRequestMessage>(); 
    var descriptor = new Moq.Mock<System.Web.Http.Controllers.HttpControllerDescriptor>(); 
    var dispatcher = new Moq.Mock<System.Web.Http.Dispatcher.IHttpControllerActivator>(); 

    Type controllerType = typeof(resources.controllers.TimeController); 

    var provider = new Moq.Mock<TimeProvider>(); 
    provider.Setup(time => time.Now).Returns(Variable); //<=Variable is initialized in the `Given` 

    TimeProvider timeProvider = provider.Object; 
    Service = new resources.controllers.TimeController(timeProvider: timeProvider); 


    //+======================================================================+ 
    //I can't figure out if there is a way to have "all calls to Create for a 
    //specific controllerType, but I do not care about the request or the 
    //descriptor" returns [Service] 
    dispatcher.Setup(context => context.Create(request.Object, descriptor.Object, controllerType)).Returns(Service); 
    //+======================================================================+ 


    dispatcher.As<IDisposable>().Setup(disposable => disposable.Dispose()); 
    return dispatcher.Object; 

} 

我最初的傾向是認爲TimeController沒有被因Create簽名的模擬,而我對路線執行未提供上述要求的事實返回和描述符匹配激活器設置中提供的模擬。

所以,我交換分派器的創建與: (請求和controllerDescriptor替換IsAny檢查) dispatcher.Setup(上下文=> context.Create(It.IsAny(),It.IsAny(),controllerType 。))返回(服務);

但我的請求沒有命中定義的路由。

任何人都知道如何讓我的模擬TimeController回到撥打client.GetAsync的電話?

+0

哎呀!我包含了來自舊嘗試的「dispatcher.Setup」代碼......所以這可能不會有很大意義。這個問題的原因是在我的dispatcher.Setup(context => context.Create ...)代碼中,'request.Object'實際上是'Moq.It.IsAny '和'descriptor.Object'實際上是'Moq.It.IsAny ',我仍然無法獲得請求。 –

回答

0

Moq不是問題。在我的Startup內部,我錯誤地將配置的屬性路由隱藏在IAppBulder.Map('NewRoute',...)之後。¹修復了無意義之後,我上面包含的代碼按照需要工作。爲了完整起見,萬一有人碰到這個跌倒的研究,我不得不改變我的客戶端代碼來獲得測試通過:

using(WebApp.Start(url: "http://localhost:9000/", startup: startup.Configuration)) 
using(var client = new System.Net.Http.HttpClient)) 
{ 
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); 
    using(System.Net.Http.HttpResponseMessage response = client.GetAsync(ResourceUnderTest.ToString()).Result) 
    { 
     if(response.StatusCode != System.Net.HttpStatusCode.OK){Assert.Fail("you suck");} 

     String content = response.Content.ReadAsStringAsync().Result; 
     DateTime actual = Newtonsoft.Json.JsonConvert.DeserializeObject<DateTime>(token); 
     Assert.AreEqual(actual, expected); 
    } 
} 

► Passed Tests (1) 
    ✔ FluxCapacitor 

..它是一樣的原代碼,除非(「應用/ json「)設置爲客戶端的Accept頭。

¹ ...I guess that my [start-stop],[start-stop],[start-stop] cycle to 
deal with drive-by interruptions finally got to me yesterday. Since I 
(obviously) didn't know exactly what the culprit was, I was concerned enough 
that the problem was in my Mock that I wanted to raise the question to the 
community before I left for the day. But walked away and looked at it again 
with a new day's eyes and it took about 30 seconds to find the culprit.