2017-01-08 6 views
4

ServiceStackで流暢な検証を使用しようとしています。私はバリデーションプラグインを追加し、バリデーターを登録しました。ServiceStackバリデータが起動しない

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または無効な承認ステータスなしで要求を作成し、私は私のサービスモデルのためのバリデータクラスを実装している

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

validatorは発動して無効な要求をキャッチしているようには見えません。

この原因をトラブルシューティングするにはどうすればよいですか?

UPDATE:訂正検証ツールは私の実際のサービスで動作していますが、単体テストでは動作していません。私はユニットテストのセットアップで正しく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検証フィルタはfull integration testを実行する必要がGlobal Request Filterで実行され、例えば:ここに私のテストのコンストラクタです

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 yeah通常、統合テストには自己ホストを使用します。そうしないと、帯域外のASP.NET WebAppインスタンスをセットアップし、それに対する統合テストを実行する必要があります – mythz

関連する問題