How to run the same Nunit test twice programmatically? - c#

I have using RestSharp to test APIs and I have a delete call that I want to run twice in the same method.
The delete call will delete with two different query params. It only can take one query param at a time so I want to call it twice with the two different query params. What is the best optimized solution to this.
The example below I am deleting with the user id 1 and I want to also delete with user id 2
[Test]
public void DeletedUser()
{
response = HttpDelete("url");
QueryParam.Add("id", 1);
Assert.statusCode(200, response);
}
I have used Andy solution to use TestCase attribute but I get an syntax error when trying to not hard code the data being used.
Error Message: "An attribute argument must be a constant expression , typeof expression or array creation expression of an attribute parameter type"
ex..
public static string data = "1"
[TestCase(data)] //Getting the error here
[Test]
public void DeletedUser(string id)
{
response = HttpDelete("url");
QueryParam.Add("id", id);
Assert.statusCode(200, response);
}
I need to run the call using two dynamic test data. The data gets generated from a Post call before the Delete call and it gets saved and serialized into a class where I have the data variables..
Here is an example of the class where the test data is stored
public class Data
{
public class UserData
{
public string id1;
public string id2;
public UserData()
{
id1 = "";
id2 = "";
}
}
}
This is the Post call and how the data is being saved.
[Test]
public void AddUser()
{
response = HttpPost("url", modle);
Data data = new Data()
data.UserData.id1 = response.content;
}
How can I now use this data.UserData.id1 in my TestCase attribute

You can make use of NUnit's [TestCase] attribute to run a test multiple times with different parameters.
Example
[TestCase(1)]
[TestCase(2)]
public void DeletedUser(int id)
{
response = HttpDelete("url");
QueryParam.Add("id", id);
Assert.statusCode(200, response);
}
You can extend this with as many parameters as needed to complete a test. For example, if you expect different responses for different IDs:
[TestCase(1, 200)]
[TestCase(2, 404)]
public void DeletedUser(int id, int expectedResponseCode)
{
response = HttpDelete("url");
QueryParam.Add("id", id);
Assert.statusCode(expectedResponseCode, response);
}
Full documentation is available here.
Update
In response to your further question about testing with dynamic data, as Charlie said you can only reference literals or literal constants from an attribute so you won't be able to use [TestCase] with dynamic data.
Instead you could use the [TestCaseSource] attribute. Create a static method that retrieves your test data and returns an array, and then quote the name of this method in the attribute.
private static int[] GetIdsToDelete() {
// UserData userData = ... read in data
return new int[] {
userData.id1,
userData.id2
}
}
[TestCaseSource(nameof(GetIdsToDelete))]
public void DeletedUser(int id)
{
response = HttpDelete("url");
QueryParam.Add("id", id);
Assert.statusCode(200, response);
}
You can find the full documentation for TestCaseSource here.

Related

Angular - Send a request with multiple models to API

I'm new in Angular, and I'm currently into a big project where many people work, so the project is based on Angular for the front end and C# for the back end.
So, the project has some services that call the backend service:
Front:
public editProfileClient(profileClient) {
return this.http
.post(this.url + '/editProfileClient', profileClient)
.pipe(map((result: ProfileClientModel) => result));
}
Back:
public async Task<ActionResult> EditProfileClient(ProfileClient profileClient)
{
//irrelevant code here
return Ok(model);
}
This is working well, but now I want to send a new model called Salary to that request, so I changed the back as:
public async Task<ActionResult> EditProfileClient(ProfileClient profileClient, Salary salary)
but I have no idea how I can send it on the front, so I receive it, but I cannot call it:
public editProfileClient(profileClient, salary) {
return this.http
.post(this.url + '/editProfileClient', profileClient, salary)
.pipe(map((result: ProfileClientModel) => result));
}
If I try to do that, the method returns an error:
Argument of type 'OperatorFunction<ProfileClientModel,
ProfileClientModel>' is not assignable to parameter of type
'OperatorFunction<ArrayBuffer, ProfileClientModel>'.
How can I achieve that?
For the API part, combine both parameters into a single object as below:
public async Task<ActionResult> EditProfileClient(EditProfileClientInputModel input)
public class EditProfileClientInputModel
{
public ProfileClient ProfileClient { get; set; }
public Salary Salary { get; set; }
}
For the front-end part:
2.1. Combine both profileClient and salary parameters into a single object and pass it.
2.2. As your API returns the response of ProfileClientModel type, you should also specify the generic type: post<T>()
public editProfileClient(profileClient, salary) {
let input = {
profileClient: profileClient,
salary: salary
};
return this.http
.post<ProfileClientModel>(this.url + '/editProfileClient', input);
}
Update
.pipe(map((result: ProfileClientModel) => result))
As per Eliseo's feedback, the pipe() should be removed as map() is used to transform the data, while you try to transform the value into the same value, which is unnecessary.

System.Text.Json JsonSerializer: Serialization and deserialization of 'System.Type' instances are not supported

I got an error related with security when I tried to deserialize by using `System.Text.Json JsonSerializer`.
What do I want to achieve?
I want to give the user controle to transalte some records in my database, so use can follow this scenario:
1- User can choose model of my class library.
2- After selecting a class, user will select a property(filed) from this class.
3- User will get list of values of the selected property up.
4- Last step is not here right now, user can edit a certian value.
This my piece of code:
MyPage.razor.cs:
[Inject]
private IGenericHttpClient<Type> HttpClient { get; set; }
private Type SelectedType { get; set; }
// First select a class [Class library] from HTML Select
private void OnTypeChnage(ChangeEventArgs args)
{
string FullName = "My.Models." + args.Value.ToString();
// Create type of selected class
SelectedType = Assemble.GetType(FullName, false);
}
//Call api to get all fields of this class
private async Task OnPropertChange(ChangeEventArgs args)
{
var list = await
HttpClient.GetJsonAsync($"/api/{SelectedType.Name}/all");
}
GenericHttpClient.cs
public async ValueTask<List<T>> GetJsonAsync(string url)
{
using HttpResponseMessage response = await _client.GetAsync(url);
ValidateResponse(response);
var conetnt = await response.Content.ReadAsStringAsync();
//I got the error down
return JsonSerializer.Deserialize<List<T>>(conetnt, new JsonSerializerOptions() { PropertyNameCaseInsensitive=true});
}
System.Text.Json does not support Type class due to security reasons. You send the full assembly name as a string and again try to construct the Type at the client end.
public async ValueTask<List<T>> GetJsonAsync(string url) this wont even compile, due to not specify generic information on method signature.
And also, your problem would come from the content of http response, otherwise, the Deserialize step should work fine.
I copied your code and make a small block that prove it.
// Define somewhere
public class GenericHttpClient
{
public List<T> GetJsonAsync<T>()
{
var content = "[{\"TestProp\": \"This is some test\"}]";
return JsonSerializer.Deserialize<List<T>>(content, new JsonSerializerOptions() { PropertyNameCaseInsensitive=true});
}
}
public class Test
{
public string TestProp { get; set; }
}
// Test it
var test = new GenericHttpClient();
var result = test.GetJsonAsync<Test>();
Like what #Mayur Ekbote mentioned up, "System.Text.Json does not support Type class due to security reasons." I will add a solution but I don't think this solution is very efficient.
Change Type to Dynamic:
[Inject]
private IGenericHttpClient<dynamic> HttpClient { get; set; }
Use JsonElement to get the value as a string:
private async Task OnPropertChange(ChangeEventArgs args)
{
var langCode = CultureInfo.CurrentCulture.Name;
PropertyValueList.Clear();
var list = await HttpClient.GetJsonAsync($"/api/{SelectedType.Name}/all");
List<object> listValue = new List<object>();
SelectedProperty = args.Value.ToString();
string fieldName = char.ToLower(SelectedProperty[0]) + SelectedProperty.Substring(1);
foreach (var item in list)
{
//Convert object to JsonElement
var val = ((JsonElement)item).GetProperty(fieldName).GetString();
PropertyValueList.Add(val);
}
}
Why is it not efficient?
Because I got a list of value String instead of list of selected class.

Receiving arbitrary JSON object in MVC method

I have a C# view class such as this:
public class DataObject
{
public int Number { get; set; }
public dynamic Data { get; set; } // <-----
}
being used in an MVC method like this
[HttpPost]
public ActionResult SaveData(DataObject request) {}
The problem is that I want to recieve multiple types of objects in the Data property of the DataObject class.
That is, I want both these to work as valid input json objects.
Type 1
{
Number: 1,
Data: {
Text: "a text"
}
}
Type 2
{
Number: 2,
Data: {
Value: 1,
Options: { 1, 2, 3, 4 }
}
}
Is there a way of doing this with either dynamic objects or some other type of json library magic (just making the property dynamic did nothing)?
All i want to do is store this data in a SQL column nvarchar field and return at a later time (through Entity Framework).
An alternate solution would be to create a view model for each type of input but as there will be 100's of variants to it creating all these views and the corresponding input methods would be cumbersome to maintain.
Adding more details as per comment request: The method is called through Angular.
pub.Save = function (jsonData) {
return $http(
{
method: "POST",
url: baseURL + "/Save",
data: { request: jsonData}, // tried this for string
// data: jsonData, // original way
timeout: 30000
}
)
.then(function (result) {
return result.data;
});
}
At the server side, DTO class must match with the same property name which the payload is carrying.
public class DataObject
{
public string test { get; set; } // <-----
}
So, your save method remains the same:
[HttpPost]
public ActionResult SaveData(DataObject request) {}
The payload json is in the object request.test but its seralized.
Deseralize it using Json Library.
How is it handling multiple different types of variables?
Deseralize it to a dynamic type as:
dynamic obj = JsonConvert.DeserializeObject(request.test, typeof(object));
//Properties within the obj are checked at run time.
if(obj.Text != null) {
//Do your thing
}
if(obj.Value != null) {
//Do your thing
}
if(obj.Options != null) {
//Do your thing
}
By converting the data to a JSON string on the client side I was able to send it to the string property and thus being able to use the same typed view for all objects.
I ended up doing this when saving the object (I'm using angular on the front end), converting the Json object to a string.
entry.Data = angular.toJson(entryData.Data, false);
And then when getting the json string back from MVC I did this to get it back to a real javascript object.
entry.Data = angular.fromJson(entry.Data);
MVC would not accept the JSON object into the text property without making it into a json string first.
Using the above method I am storing data like this in my database:
"{\"Value\":123,\"Currency\":\"EUR\"}"

Can I manually hard code a JSON object to be returned by ASP.NET web API?

I'm used to doing this in Django (similar to Ruby on Rails) where in some cases I need to hard code a JSON response object for the client to be able to interpret, but I've been searching everywhere online on figuring out how to do this with ASP.NET web API and I can't find anything on this, ASP.NET web API seems to be forcing me to create a class to represent a JSON response for every URI controller.
For example, here's the only way I know for manually creating a JSON response:
1.) I first need to create the class to represent the response object
public class XYZ_JSON
{
public string PropertyName { get; set; }
public string PropertyValue { get; set; }
}
2.) Then I need to properly write up the URI controller that'll return an "XYZ_JSON" that I've just defined above:
// GET: api/ReturnJSON
public XYZ_JSON Get()
{
XYZ_JSON test = new XYZ_JSON { PropertyName = "Romulus", PropertyValue = "123123" };
return test;
}
Will result with an http response of something like:
200 OK
{"PropertyName":"Romulus", "PropertyValue":"123123"}
This whole class to JSON design pattern is cool and all, but it's not helpful and actually makes things much worse when trying to return a class as a JSON object with many classes within it such as:
public class XYZ_JSON
{
public string PropertyName { get; set; }
public string PropertyValue { get; set; }
public List<ComplexObject> objects { get; set; } // <- do not want
}
The JSON response object above isn't that complex, but for what I'm trying to accomplish I'll have to put a list of classes within a list of classes within a list of classes, and I can't develop it in this awkward way unless I spend a week on it which is just ridiculous.
I need to be able to return a JSON response in this kind of fashion:
// GET: api/ReturnJSON
public JSON_Response Get(string id)
{
// do some SQL querying here to grab the model or what have you.
if (somethingGoesWrong = true)
return {"result":"fail"}
else
return {"result":"success","value":"some value goes here"}
}
The design pattern above is what I'm trying to accomplish with ASP.NET web API, a very simply way to return a semi-hard coded JSON response object which would allow me to return very unique and dynamic responses from a single URI. There's going to be many use cases where a list of up to 8 completely unique Class objects will be returned.
Also, If what I'm trying to accomplish is the backwards way of doing things than that's fine. I've released a very successful and stable iOS application with a flawless Django backend server handling things this way perfectly without any issues.
Can someone explain to me how I can return a simple hard coded JSON response using the ASP.NET web API?
Thanks!
You can create anonymous types in C#, so you can use one of these to produce your hard-coded result. For example:
return new JsonResult
{
Data = new
{
result = "success",
value = "some value"
}
};
To clarify, the above code is for ASP.NET MVC. If you're using Web API, then you can just return the data object, or use an IHttpActionResult. The anonymous type part (the new {}) stays the same.
Use an anonymous object.
public object Get(string id)
{
// do some SQL querying here to grab the model or what have you.
if (somethingGoesWrong = true)
return new {result = "fail"}
else
return new {result = "success", value= "some value goes here"}
}
You can use a generic JObject to return your values without constructing a complete class structure as shown below
public JObject Get(int id)
{
return JsonConvert.DeserializeObject<JObject>(#"{""result"":""success"",""value"":""some value goes here""}");
}
For hard coded response, why not just do something like below. The JSON content will be returned without being surrounded by quotation marks.
public HttpResponseMessage Get()
{
string content = "Your JSON content";
return BuildResponseWithoutQuotationMarks(content);
}
private HttpResponseMessage BuildResponseWithoutQuotationMarks(string content)
{
var response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(content);
return response;
}
private HttpResponseMessage BuildResponseWithQuotationMarks(string content)
{
var response = Request.CreateResponse(HttpStatusCode.OK, content);
return response;
}
// GET: api/ReturnJSON
public JsonResult Get()
{
return Json(new { Property1 = "Value1", Property2 = "Value2" });
}
You can return json using JsonResult class. and the Json() method takes anonymous object so you don't need to create a class.

Creating POST method in Web API

I'm developing a web service, using WEB .API. I'm following the example, which include:
public HttpResponseMessage PostProduct(Product item)
{
item = repository.Add(item);
var response = Request.CreateResponse<Product>(HttpStatusCode.Created, item);
string uri = Url.Link("DefaultApi", new { id = item.Id });
response.Headers.Location = new Uri(uri);
return response;
}
for creating a POST method, allowing a client to send data in POST in ordert to insert these data in the database (I'm using Entity Framework).
What I want do, however, is slightly difference, since the data I want pass in post to the web service are not associated to any object of database: I have some data that should be write in more then one table. For example:
{"activity":"sport","customValue":"22","propertyIndex":"122-x"}
The activty value (sport) should be writed on one table, while the others two parameters (customValue e properyIndex) shouldbe writed on another table.
So I think I need to parse the json file received in POST and then execute the two insert operation.
How can I perform this task?
You need to create an object in web API project with Activity, CustomValue, PropertyIndex properties:
public class MyTestClass
{
public string Activity { get; set; }
public string CustomValue { get; set; }
public string PropertyIndex { get; set; }
}
and HttpPost will be:
[HttpPost]
public HttpResponseMessage Post(MyTestClass class)
{
// Save Code will be here
return new HttpResponseMessage(HttpStatusCode.OK);
}
Product class should have Activity, CustomValue and PropertyIndex properties to get bind with posted data.
[HttpPost]
[ActionName("alias_for_action")]
public HttpResponseMessage PostProduct([FromBody] Product item)
{
//your code here
var response = new HttpResponseMessage(HttpStatusCode.Created)
{
Content = new StringContent("Your Result")
};
return response;
}
Yes if you want to update two tables in database using Entity Framework then you have to execute two insert operations.

Categories