2017-01-08 75 views
4

我想在ServiceStack中使用流暢的驗證。我已經添加了驗證插件並註冊了驗證器。ServiceStack驗證程序沒有觸發

Plugins.Add(new ValidationFeature()); 
    container.RegisterValidators(typeof(CreateLeaveValidator).Assembly); 

我實現了一個驗證器類爲我的服務模式:

public class CreateLeaveValidator : AbstractValidator<CreateLeave> 
{ 
    public CreateLeaveValidator() 
    { 
     RuleFor(cl => cl.StudentId).NotEmpty(); 
     RuleFor(cl => cl.LeaveDepart).NotEmpty().GreaterThan(DateTime.Now).WithMessage("Leave must begin AFTER current time and date."); 
     RuleFor(cl => cl.LeaveReturn).NotEmpty().GreaterThan(cl => cl.LeaveDepart).WithMessage("Leave must end AFTER it begins."); 
     RuleFor(cl => cl.ApprovalStatus).Must(status => (("P".Equals(status)) || ("C".Equals(status)) || ("A".Equals(status)) || ("D".Equals(status)))); 
    } 
} 

服務模式:

[Route("/leaves", "POST")] 
    public class CreateLeave : IReturn<LeaveResponse>, IUpdateApprovalStatus 
    { 
     public int StudentId { get; set; } 
     public DateTime RequestDate { get; set; } 
     public DateTime LeaveDepart { get; set; } 
     public DateTime LeaveReturn { get; set; } 
     public string Destination { get; set; } 
     public string HostRelationship { get; set; } 
     public string Address { get; set; } 
     public string City { get; set; } 
     public string State { get; set; } 
     public string Postal { get; set; } 
     public string Hostphone { get; set; } 
     public string Cellphone { get; set; } 
     public string Transport { get; set; } 
     public string Driver { get; set; } 
     public string Companions { get; set; } 
     public string Reason { get; set; } 
     public string ApprovalStatus { get; set; } 
     public DateTime ApprovalDate { get; set; } 
     public string ApprovalComment { get; set; } 
     public string ApprovalReason { get; set; } 
     public int ApprovalUser { get; set; } 
    } 

但是,當我創建一個沒有StudentId或無效的審批狀態,一個請求驗證器不會觸發並捕獲無效請求。

我該如何解決這個問題的原因?

更新:更正它似乎驗證工作與我的實際服務,但不是在我的單元測試。我猜測我不能在單元測試設置中正確配置我的apphost。下面是我的測試構造:

public LeaveTests() 
    { 
     Licensing.RegisterLicense(@"[license key]"); 

     appHost = new BasicAppHost(typeof(ApiServices).Assembly).Init(); 
     ServiceStack.Text.JsConfig.DateHandler = ServiceStack.Text.DateHandler.ISO8601; 
     appHost.Plugins.Add(new ValidationFeature());    
     appHost.Container.RegisterValidators(typeof(CreateLeaveValidator).Assembly); 

    } 

回答

4

ServiceStack驗證過濾器在Global Request Filter需要一個full integration test運行執行,例如:

public class MyIntegrationTests 
{ 
    ServiceStackHost appHost; 
    public MyIntegrationTests() 
    { 
     appHost = new AppHost() 
      .Init() 
      .Start("http://localhost:8000/"); 
    } 

    [OneTimeTearDown] void OneTimeTearDown() => appHost.Dispose(); 

    [Test] 
    public void Execute_validation_filters() 
    { 
     var client = new JsonServiceClient("http://localhost:8000/"); 

     try 
     { 
      var response = client.Post(new CreateLeave { ... }); 
     } 
     catch(WebServiceException ex) 
     { 
      //... 
     } 
    } 
} 
+0

我們的項目是設置爲使用IIS的主機,所以我們增加了一個單獨的基於AppSelfHostBase的集成測試。這是通常的做法嗎? – scotru

+1

@scotru是的我通常只使用自己的主機進行集成測試,否則你需要設置一個ASP.NET WebApp實例帶外並運行集成測試 – mythz