Reputation: 399
I am trying to unit test a class I have written which overrides the Authorization attribute. I am getting below error on this class. below is the code for the class.
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);
}
}
}
class which is being tested is as below..
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;
}
}
}
I ma getting below error while running the test:
One time setup no suitable constructor was found.
Upvotes: 1
Views: 1687
Reputation: 151586
The exception means that the test framework can't instantiate your test class. This is because it doesn't contain a parameterless constructor.
The class containing your unit tests shouldn't inherit from the class you're trying to test.
Remove the : AuthorizeCreditNote
and the CustomAuthorizeAttributeTests(string Entity, string Key)
constructor.
Upvotes: 2