【问题标题】:One time setup no suitable constructor was found一次设置未找到合适的构造函数
【发布时间】:2018-09-06 08:45:58
【问题描述】:

我正在尝试对我编写的覆盖 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】:

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

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

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

    【讨论】:

    • 我不能,因为我需要在需要测试的类中测试受保护的方法。此方法受保护 override bool AuthorizeCore(HttpContextBase httpContext)
    • 然后致电OnAuthorize()。见stackoverflow.com/questions/18701433/…。或者创建一个无参数的构造函数。
    • 我添加了一个无参数构造函数,但我在 AuthorizeCore 上看到无法访问的错误。
    猜你喜欢
    • 1970-01-01
    • 2022-01-11
    • 2016-03-24
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多