2013-10-10 17 views
1

我有一个ServiceStack AppHostHttpListenerBase在我的测试配置与运行测试的,像这样的宗旨:ServiceStack AppHostHttpListenerBase无法解析服务的依赖关系

public class UserProfileBehaviours : BaseTest<UserProfileService> 
{ 
    [Test] 
    public void Can_access_secure_service() 
    { 
     var client = GetClientWithUserPassword(); 

     var result = ((IRestClient)client).Get(new Dto.View.Requests.UserProfileRequest 
      { 
       Slug = "user" 
      }); 

     result.Result.UserAccount.Username.Should().Be("user"); 
    } 
} 

我BaseTest样子:

[TestFixture] 
public class BaseTest<T> 
{ 
    protected TestAppHostHttpListenerBase<T> AppHost; 
    private const string ListeningOn = "http://localhost:82/"; 
    private const string UserName = "user"; 
    private const string Password = "[email protected]"; 
    protected readonly Container Container; 

    public BaseTest() 
    { 
     Container = new Funq.Container 
     { 
      Adapter = new WindsorContainerAdapter() 
     }; 
    } 

    [TestFixtureSetUp] 
    public void OnTestFixtureSetUp() 
    { 
     AppHost = new TestAppHostHttpListenerBase<T>(); 

     AppHost.Init(); 

     AppHost.Start(ListeningOn); 
    } 

    [TestFixtureTearDown] 
    public void OnTestFixtureTearDown() 
    { 
     AppHost.Dispose(); 
    } 

    protected IServiceClient GetClient() 
    { 
     return new JsonServiceClient(ListeningOn); 
    } 

    protected IServiceClient GetClientWithUserPassword() 
    { 
     return new JsonServiceClient(ListeningOn) 
     { 
      UserName = UserName, 
      Password = Password 
     }; 
    } 
} 

然后我WindsorContainerAdapter:

最后我的TestAppHostH ttpListener

public class TestAppHostHttpListenerBase<T> : AppHostHttpListenerBase 
{ 
    public const string WebHostUrl = "http://localhost:82/"; 
    private InMemoryAuthRepository _userRep; 
    private const string UserName = "user"; 
    private const string Password = "[email protected]"; 
    public const string LoginUrl = "specialLoginPage.html"; 

    public TestAppHostHttpListenerBase() 
     : base("Validation Tests", typeof(T).Assembly) 
    { 
    } 

    public override void Configure(Container container) 
    { 

     var appSettings = new AppSettings(); 

     SetConfig(new EndpointHostConfig { WebHostUrl = WebHostUrl }); 

     Plugins.Add(new AuthFeature(
         () => 
         new AuthUserSession(), 
         new IAuthProvider[] 
          { 
           new BasicAuthProvider(), 
           new CredentialsAuthProvider(), 
           new TwitterAuthProvider(appSettings), 
           new FacebookAuthProvider(appSettings) 
          }, "~/" + LoginUrl)); 

     container.Register<ICacheClient>(new MemoryCacheClient()); 
     _userRep = new InMemoryAuthRepository(); 
     container.Register<IUserAuthRepository>(_userRep); 
     CreateUser(1, UserName, null, Password, new List<string> { "TheRole" }, new List<string> { "ThePermission" }); 
    } 

    private void CreateUser(int id, string username, string email, string password, List<string> roles = null, List<string> permissions = null) 
    { 
     string hash; 
     string salt; 
     new SaltedHash().GetHashAndSaltString(password, out hash, out salt); 

     if (_userRep.GetUserAuthByUserName(username) == null) 
     { 
      _userRep.CreateUserAuth(new UserAuth 
      { 
       Id = id, 
       DisplayName = "DisplayName", 
       Email = email ?? "[email protected]{0}.com".Fmt(id), 
       UserName = username, 
       FirstName = "FirstName", 
       LastName = "LastName", 
       PasswordHash = hash, 
       Salt = salt, 
       Roles = roles, 
       Permissions = permissions 
      }, password); 
     } 
    } 
} 

当容器被配置,我可以看到,然而对于UserAccountRepository一个组件 - 如果该组件是一个UserProfileService的扶养客户端收到异常说的自动装配Autowired扶养无法解析。 我不明白的是AppHostHttpListenerBase从哪里得到它的容器?

我的Windsor适配器永远不会被要求到Resolve存储库的组件。

我怎样才能给AppHostHttpListenerBase容器,以便它可以解决这些依赖关系?或者我需要另一种方式进行配置?

回答

1

尝试在TestAppHostHttpListenerBase.Configure方法的顶部设置容器适配器,而不是内的BaseTest类:

public override void Configure(Container container) 
{ 
    container.Adapter = new WindsorContainerAdapter(); 
    ... 
} 

给予TestAppHostHttpListenerBase.Configure方法的Container对象在TestAppHostHttpListenerBase的基部构造创建的;您无法直接控制其创作。这是你需要与适配器定制容器实例等

如果您还需要使用相同的IoC容器在UserProfileBehaviours或其他单元测试类,我想你可以通过静态EndpointHost.Container属性引用它,消除在BaseTest构造函数中创建的额外容器实例。正如上面所暗示的那样,在实例化OnTestFixtureSetUp中的AppHost对象后,容器对象将变为可用。

+0

BOOM !!多谢,伙计 – iwayneo