C# 一次安装未找到合适的构造函数
我正在尝试对我编写的重写授权属性的类进行单元测试。我在这门课上犯了错误。下面是该类的代码C# 一次安装未找到合适的构造函数,c#,unit-testing,C#,Unit Testing,我正在尝试对我编写的重写授权属性的类进行单元测试。我在这门课上犯了错误。下面是该类的代码 namespace MyApplicationTests.Unit.Attribute { [TestFixture] public class CustomAuthorizeAttributeTests : AuthorizeCreditNote { private bool hasAccessOnRequestedData; public Cus
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);
}
}
}
名称空间MyApplicationTests.Unit.Attribute
{
[测试夹具]
公共类CustomAuthorizeAttributeTests:AuthorizeCreditNote
{
私有bool hasAccessOnRequestedData;
公共CustomAuthorizeAttributeTests(字符串实体,字符串键):基(实体,键)
{
}
[测试]
public void customauthorizationattributes_throuwnullargumentexception_当参数发送()时
{
var authContext=新授权上下文();
字符串实体=string.Empty;
string Key=string.Empty;
var attr=新的信用记录(实体、密钥);
抛出(()=>attr.OnAuthorization(authContext));
}
[测试]
public void customauthorizationattributes\u ReturnFalse\u如果上下文详细信息不受审查()
{
var parm1=“TestParm1”;
var parm2=“TestParm2”;
var attr=新的信用票据(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;
}
}
}
namespace myApplication.Web.Supporting.Attributes.Finance
{
[AttributeUsageAttribute(AttributeTargets.Method,Inherited=true,AllowMultiple=false)]
公共类authorizeCredit注意:AuthorizeAttribute
{
受保护字符串实体;
公共字符串实体
{
获取{返回this.entity;}
}
受保护的字符串密钥;
公共字符串密钥
{
获取{返回this.key;}
}
私有bool hasAccessOnRequestedData;
公共字符串值{get;set;}
公共注释(字符串实体、字符串键)
{
this.entity=实体;
this.key=key;
}
受保护的覆盖bool AuthorizeCore(HttpContextBase httpContext)
{
hasAccessOnRequestedData=false;
if(string.IsNullOrEmpty(实体)| | string.IsNullOrEmpty(键))
抛出新ArgumentNullException(实体==null?实体:键);
var isAuthorized=base.AuthorizeCore(httpContext);
如果(未授权)
{
var_customerContext=DependencyResolver.Current.GetService();
if(Entity==attributehelp.CreditNote.Entity)
{
var entityKeyValue=HttpContext.Current.Request.Params[key];
var entityReader=DependencyResolver.Current.GetService();
如果(entityReader!=null)
{
var entityResult=entityReader.Get(entityKeyValue.ToString());
var expectedCustomerNumber=\u customerContext.LoggedInCustomer.MasterAccount?.CustomerNumber???\u customerContext.LoggedInCustomer.CustomerNumber;
hasAccessOnRequestedData=(expectedCustomerNumber==entityResult.CustomerNumber)?true:false;
返回hasAccessOnRequestedData;
}
返回true;
}
}
返回hasAccessOnRequestedData;
}
}
}
运行测试时出现以下错误:
一次安装未找到合适的构造函数
异常意味着测试框架无法实例化您的测试类。这是因为它不包含无参数构造函数 包含单元测试的类不应从您尝试测试的类继承
删除
:AuthorizeCreditNote
和CustomAuthorizeAttributeTests(字符串实体,字符串键)
构造函数。异常意味着测试框架无法实例化测试类。这是因为它不包含无参数构造函数
包含单元测试的类不应从您尝试测试的类继承
删除
:AuthorizeCreditNote
和CustomAuthorizeAttributeTests(字符串实体、字符串键)
构造函数。我不能,因为我需要在需要测试的类中测试受保护的方法。此方法受保护,覆盖bool AuthorizeCore(HttpContextBase httpContext),然后调用OnAuthorize()
。看见或者创建一个无参数构造函数。我添加了一个无参数构造函数,但我仍然看到AuthorizeCore上的错误,无法访问它。我不能,因为我需要在需要测试的类中测试一个受保护的方法。此方法受保护,覆盖bool AuthorizeCore(HttpContextBase httpContext),然后调用OnAuthorize()
。看见或者创建一个无参数构造函数。我已经添加了一个无参数构造函数,但是我在AuthorizeCore上看到了无法访问的错误。