一次设置找不到合适的构造函数

时间:2018-09-06 08:45:59

标签: c# unit-testing

我正在尝试对已编写的覆盖Authorization属性的类进行单元测试。我在这堂课上遇到了错误。下面是该类的代码。

namespace MyApplicationTests.Unit.Attribute
{
    [TestFixture]
    public class CustomAuthorizeAttributeTests : AuthorizeCreditNote
    {
        private bool hasAccessOnRequestedData;

        public CustomAuthorizeAttributeTests(string Entity, string Key) : base(Entity, Key)
        {
        }

        [Test]
        public void CustomAuthorizeAttributes_ThrowNullArgumentException_WhenParametersAreMissing()
        {

        var authContext = new AuthorizationContext();
        string Entity = string.Empty;
        string Key = string.Empty;
        var attr = new AuthorizeCreditNote(Entity, Key);

        Assert.Throws<Exception>(() => attr.OnAuthorization(authContext));
        }

        [Test]
        public void CustomAuthorizeAttributes_ReturnFalse_IfContextUserDetailsAreNotBeingReviewed()
        {
            var parm1 = "TestParm1";
            var parm2 = "TestParm2";
            var attr = new AuthorizeCreditNote(parm1, parm2);
            Assert.AreEqual(attr.AllowMultiple, false);

        }
    }
}

正在测试的类如下。

namespace myApplication.Web.Supporting.Attributes.Finance
{
    [AttributeUsageAttribute(AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
    public class AuthorizeCreditNote : AuthorizeAttribute
    {
        protected string entity;
        public string Entity
        {
            get { return this.entity; }
        }

        protected string key;
        public string Key
        {
            get { return this.key; }
        }

        private bool hasAccessOnRequestedData;


        public string Value { get; set; }
        public AuthorizeCreditNote(string Entity, string key)
        {
            this.entity = Entity;
            this.key = key;
        }

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            hasAccessOnRequestedData = false;
            if (string.IsNullOrEmpty(Entity) || string.IsNullOrEmpty(Key))
                throw new ArgumentNullException(entity == null ? entity : key);

            var isAuthorized = base.AuthorizeCore(httpContext);
            if (isAuthorized)
            {
                var _customerContext = DependencyResolver.Current.GetService<ICustomerContext>();

                if (Entity == AttributeHelper.CreditNote.Entity)
                {
                    var entityKeyValue = HttpContext.Current.Request.Params[key];
                    var entityReader = DependencyResolver.Current.GetService<ICreditReader>();

                    if (entityReader != null)
                    {
                        var entityResult = entityReader.Get(entityKeyValue.ToString());

                        var expectedCustomerNumber = _customerContext.LoggedInCustomer.MasterAccount?.CustomerNumber ?? _customerContext.LoggedInCustomer.CustomerNumber;
                        hasAccessOnRequestedData = (expectedCustomerNumber == entityResult.CustomerNumber) ? true : false;
                        return hasAccessOnRequestedData;
                    }

                    return true;
                }
            }
            return hasAccessOnRequestedData;
        }
    }
}

运行测试时,我可能遇到以下错误:

  

一次设置没有找到合适的构造函数。

1 个答案:

答案 0 :(得分:1)

该异常意味着测试框架无法实例化您的测试类。这是因为它不包含无参数构造函数。

包含单元测试的类不应从您要测试的类继承。

删除: AuthorizeCreditNoteCustomAuthorizeAttributeTests(string Entity, string Key)构造函数。