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

问题描述 投票:0回答:1

我正在尝试对我编写的一个类进行单元测试,该类覆盖了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;
        }
    }
}

我在运行测试时遇到错误:

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

c# unit-testing
1个回答
1
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.