ServiceStack验证不总是射击(ServiceStack Validation Not Al

2019-08-17 10:06发布

所以我想建立一个端到端的集成测试套件与RavenDB和ServiceStack,但我遇到了一个很奇怪的问题,即验证不上一些要求运行。 这真的很奇怪,我不知道我做错了。 我使用NCrunch。 有时,测试通过,有时失败。

希望这是一个简单的办法和一些骨为首我做的。

您可以下载整个项目上http://github.com/khalidabuhakmeh/endtoend

你并不需要比VS2012和NuGet包还原启用的任何其他。

更新:我决定在这两个NCrunch和ReSharper的测试运行运行此两者给予同样的结果[见下图]。

更新来更新:我想这可能是的xUnit,所以我试图使用NUnit。 都能跟得上还是同样的问题。

**另一个更新:在控制台将写入按user1901853的要求。 这是结果“。

最新:在RequestFilters越来越消灭了,我不知道为什么。 看起来这可能是一个线程问题,但我看不到的地方。

我APPHOST使用AppHostListenerBase。

    using EndToEnd.Core;
    using Funq;
    using Raven.Client;
    using ServiceStack.ServiceInterface.Validation;
    using ServiceStack.WebHost.Endpoints;

    namespace EndToEnd
    {
        public class TestAppHost
            : AppHostHttpListenerBase
        {
            private readonly IDocumentStore _documentStore;

            public TestAppHost(IDocumentStore documentStore)
                : base("Test AppHost Api", typeof(TestAppHost).Assembly)
            {
                _documentStore = documentStore;
            }

            public override void Configure(Container container)
            {
                ServiceStack.Text.JsConfig.EmitCamelCaseNames = true;

                // Register RavenDB things
                container.Register(_documentStore);
                container.Register(c =>
                {
                    var db = c.Resolve<IDocumentStore>();
                    return db.OpenSession();
                }).ReusedWithin(ReuseScope.Request);

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

                // todo: register all of your plugins here
                AuthConfig.Start(this, container);
            }
        }
    }

我对我所有的测试基础测试类看起来是这样的:

    using Raven.Client;
    using Raven.Client.Indexes;
    using Raven.Tests.Helpers;
    using ServiceStack.Authentication.RavenDb;
    using ServiceStack.ServiceClient.Web;
    using ServiceStack.ServiceInterface.Auth;

    namespace EndToEnd
    {
        public abstract class ServiceStackTestBase
            : RavenTestBase
        {
            protected IDocumentStore DocumentStore { get; set; }
            protected TestAppHost Host { get; set; }
            protected JsonServiceClient Client { get; set; }

            protected const string ListeningOn = "http://localhost:1337/";

            protected string Username { get { return "testuser"; } }
            protected string Password { get { return "password"; } }

            protected ServiceStackTestBase()
            {
                DocumentStore = NewDocumentStore();
                IndexCreation.CreateIndexes(typeof(ServiceStackTestBase).Assembly, DocumentStore);
                IndexCreation.CreateIndexes(typeof(RavenUserAuthRepository).Assembly, DocumentStore);

                Host = new TestAppHost(DocumentStore);
                Host.Init();
                Host.Start(ListeningOn);

                Client = new JsonServiceClient(ListeningOn)
                {
                    AlwaysSendBasicAuthHeader = true,
                    UserName = Username,
                    Password = Password
                };

                RegisterUser();

                WaitForIndexing(DocumentStore);
            }

            private void RegisterUser()
            {
                Client.Send(new Registration
                {
                    UserName = Username,
                    Password = Password,
                    DisplayName = "Test User",
                    Email = "test@test.com",
                    FirstName = "test",
                    LastName = "user"
                });
            }

            public override void Dispose()
            {
                DocumentStore.Dispose();
                Host.Dispose();
            }
        }
    }

我的测试类如下所示:

    using System;
    using EndToEnd.Core;
    using FluentAssertions;
    using ServiceStack.FluentValidation;
    using ServiceStack.ServiceClient.Web;
    using ServiceStack.ServiceInterface.Auth;
    using Xunit;

    namespace EndToEnd
    {
        public class RegistrationTests
            : ServiceStackTestBase
        {
            [Fact]
            public void Throws_validation_exception_when_bad_widget()
            {
                var validator = Host.Container.Resolve<IValidator<CreateWidget>>();
                validator.Should().NotBeNull();


                try
                {
                    var response = Client.Post(new CreateWidget
                    {
                        Name = null
                    });
                    // It get's here every once in a while
                    throw new Exception("Should Not Get Here!");
                }
                catch (WebServiceException wex)
                {
                    wex.StatusCode.Should().Be(400);
                    wex.ErrorMessage.Should().Be("'Name' should not be empty.");
                }
            }
        }
    }

我的代码看起来像这样的服务:

    using System;
    using Raven.Client;
    using ServiceStack.FluentValidation;
    using ServiceStack.ServiceHost;
    using ServiceStack.ServiceInterface;
    using ServiceStack.ServiceInterface.ServiceModel;

    namespace EndToEnd.Core
    {
        [Authenticate]
        public class WidgetsService
            : Service
        {
            private readonly IDocumentSession _session;

            public WidgetsService(IDocumentSession session)
            {
                _session = session;
            }

            public CreateWidgetResponse Post(CreateWidget input)
            {
                var widget = new Widget { Name = input.Name };
                _session.Store(widget);
                _session.SaveChanges();

                return new CreateWidgetResponse { Widget = widget };
            }
        }

        [Route("/widgets", "POST")]
        public class CreateWidget : IReturn<CreateWidgetResponse>
        {
            public string Name { get; set; }
        }

        public class CreateWidgetResponse
        {
            public CreateWidgetResponse()
            {
                ResponseStatus = new ResponseStatus();
            }

            public Widget Widget { get; set; }
            public ResponseStatus ResponseStatus { get; set; }   
        }

        public class Widget
        {
            public Widget()
            {
                Created = DateTimeOffset.UtcNow;
            }

            public string Id { get; set; }
            public string Name { get; set; }
            public DateTimeOffset Created { get; set; }
        }

        public class CreateWidgetValidator : AbstractValidator<CreateWidget>
        {
            public CreateWidgetValidator()
            {
                RuleFor(m => m.Name).NotEmpty();
            }
        }
    }

Answer 1:

我没有复制你的环境的能力,但在VS2010运行时,使用的是.NET 4,NUnit的和ReSharper的测试运行我一直无法重现您的“验证不点火”的问题。 我已经运行测试30+倍。 一对夫妇的原因,我能想到的验证不点火将没有添加插件或插件不注册验证过滤器。 2. if statements下面可能给你一些“内省”如果任何的我列出的案件的问题。 希望这是有所帮助的。

if (!TestAppHost.Instance.Plugins.Any(x => x.GetType() == typeof(ValidationFeature)))
{
    Console.Write("Validation Plugin is not added");
    //TestAppHost.Instance.Plugins.Add(new ValidationFeature());
}

if (!TestAppHost.Instance.RequestFilters.Any(x => x.Target.ToString() == "ServiceStack.ServiceInterface.Validation.ValidationFilters"))
{
    Console.Write("No validation request filter");
   //TestAppHost.Instance.Container.RegisterValidators(typeof(CreateWidgetValidator).Assembly);
}

下面是我packages.config所以你可以看到在我们的环境中的差异。

<packages>
  <package id="FluentAssertions" version="2.0.1" targetFramework="net40" />
  <package id="NUnit" version="2.6.2" targetFramework="net40" />
  <package id="RavenDB.Client" version="2.0.2261" targetFramework="net40" />
  <package id="RavenDB.Database" version="2.0.2261" targetFramework="net40" />
  <package id="RavenDB.Embedded" version="2.0.2261" targetFramework="net40" />
  <package id="RavenDB.Tests.Helpers" version="2.0.2261" targetFramework="net40" />
  <package id="ServiceStack" version="3.9.38" targetFramework="net40-Client" />
  <package id="ServiceStack.Authentication.RavenDB" version="3.9.35" targetFramework="net40" />
  <package id="ServiceStack.Common" version="3.9.38" targetFramework="net40-Client" />
  <package id="ServiceStack.OrmLite.SqlServer" version="3.9.39" targetFramework="net40-Client" />
  <package id="ServiceStack.Redis" version="3.9.38" targetFramework="net40-Client" />
  <package id="ServiceStack.Text" version="3.9.38" targetFramework="net40-Client" />
  <package id="xunit" version="1.9.1" targetFramework="net40" />
</packages>


文章来源: ServiceStack Validation Not Always Firing