2016-06-13 55 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.