0

I have a class with a constructor like so:

public class SsoAuthenticationService : ISsoAuthenticationService
{
  public SsoAuthenticationService(ILoginManager manager)
    {
        _manager = manager;
    }
}

And also the manager class has a constructor like so:

 public class LoginManager:ILoginManager
 {
    private ILoginRepository login;

    public LoginManager(ILoginRepository loginRepository)
    {
        login = loginRepository;
    }
  }

I am trying to write a unit test against the SSoAuthenticationService class

    public void CreateLogin_Returns_Login()
    {
        //arrange
        var mocManager = new Mock<ILoginManager>();

        ILoginManager m = mocManager.Object;

        //fails at the next line
        var svc = new SsoAuthenticationService(mocManager.Object);
        var request = new CreateSsoLoginRequest()
                          {
                              EmailAddress = "",
                              Password = "",
                              SecurityQuestionAnswer = "",
                              SecurityQuestionId = 0,
                              SiteIdentifier = "",
                              Username = ""
                          };

        //act
        var response = svc.CreateSsoLogin(request);

        //assert
        response.Should().NotBeNull();
    }

This gives an error when it tries to instantiate the SsoAuthenticationService class ("Value cannot be null.\r\nParameter name: value"), and I believe that the problem is that I need to also mock the IRepository class as well, but I'm not sure how the mocking code should then look.

Edit: Full exception info

The type initializer for 'Progressive.Sso.WebServices.SsoAuthenticationService' threw an exception.
Value cannot be null.\r\nParameter name: value
at Progressive.Sso.WebServices.SsoAuthenticationService..ctor(ILoginManager manager)
at Progressive.Sso.Tests.Service_Methods.ServiceTest.CreateLogin_Returns_Login() in d:\tfs\sso\Dev\Dev\Src\SSO Web Services\Progressive.Sso.Tests\Service Methods\ServiceTest.cs:line 29

Can anyone help me out?

4

1 回答 1

0

我找到了解决方案,问题与 Moq 或构造函数依赖项无关。

我声明了以下两个成员变量

private static readonly bool _useOperationTimer = SsoConfig.UseServiceOperationTimer;
private static readonly bool _logRequestData = SsoConfig.LogRequestData;

静态方法调用是问题的原因,如果它们被注释掉一切正常。

于 2013-10-23T02:46:45.707 回答