Hansa
Hansa

Reputation: 11

Unit Testing Error when i use JsonRequired Attribute in Dto Class

My unit testing is failed when i change Required field to JsonRequired my Dto Class

class name = customerDto

[JsonRequired]
public int ID {get; set;}
[JsonRequired]
public int Name{get; set;}

When use Required Attribute it was passed.

My Test Code

var data = new customerDto {ID = ValidID};
byte[] byteArray = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data));
httpContext.Request.Body = new MemoryStream(byteArray);

in above line debug will stop. i need to fix this.

My unit testing failed when i use JsonRequired Attribute. But it will successfully debug when i use Required attribute

Upvotes: 1

Views: 94

Answers (1)

Philip Smith
Philip Smith

Reputation: 2801

Your customerDto is set to require the Name property to be given a value when converting to JSON. Your test converts the customerDto to JSON. Your test does not set a value on the Name property.

Either modify the test to supply a value to the Name property or remove the JsonRequired attribute from the Name property.

Your test does not do anything that would excercise the Required attribute. That's why you did not have problems when using that attribute.

Upvotes: 1

Related Questions