i could realy need some help.
Im coding a small WebApi Program.
While Unit testing i noticed i cant send a list of Elements to my post Method.
First take a look at my Unit Test:
[TestMethod]
public async Task PostWithExpandoObjectSerilasationToDataType()
{
//Define an imaginary Car Object
List<dynamic> listExpando = new List<dynamic>();
dynamic obj1 = new ExpandoObject();
obj1.Attribute = "PS";
obj1.DataType = "Integer";
obj1.EntityId = "3";
obj1.DefaultValue = "";
dynamic obj2 = new ExpandoObject();
obj2.Attribute = "Color";
obj2.DataType = "Text Only";
obj2.EntityId = "3";
obj2.DefaultValue = "";
dynamic obj3 = new ExpandoObject();
obj3.Attribute = "Km";
obj3.DataType = "Number";
obj3.EntityId = "3";
obj3.DefaultValue = "1.3";
listExpando.Add(obj1);
listExpando.Add(obj2);
listExpando.Add(obj3);
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.RegisterConverters(new JavaScriptConverter[] { new ExpandoJsonconverter() });
string jsonOfTest = javaScriptSerializer.Serialize(listExpando);
// arrange
UnityContainer container = UnityConfig.RegisterComponents();
var start = new Startup(container);
using (TestServer server = TestServer.Create(builder => start.Configuration(builder)))
{
using (var client = new HttpClient(server.Handler))
{
HttpContent content = new StringContent(jsonOfTest, Encoding.UTF8, "application/json");
var request = new HttpRequestMessage
{
RequestUri = new Uri("http://testserver/odata/Attributes"),
Method = HttpMethod.Post,
Content = content
};
string jsonContent = content.ReadAsStringAsync().Result;
request.Headers.Add("Prefer", "return=representation");
// act
HttpResponseMessage response = await client.SendAsync(request);
Attributes result = response.Content.ReadAsAsync<Attributes>().Result;
// assert
Assert.IsNotNull(response);
Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
Assert.IsNotNull(result, "No result content found");
Assert.IsNotNull(result.Attribute);
}
}
}
Next here is my Post Method:
public IHttpActionResult Post([ModelBinder]IEnumerable<Attributes> att)
{....}
When it comes to the Post Method att is alway null
When trying the Service with Rest i get this message:
"message": "Collection(EAVService.Entities.Attributes) is not an entity type. Only entity types are supported."
Googled and tried different things but i didnt found a working solution
Any one there who can help me to solve my problem? :)
Best Regards Andre