0

While writing unit test I got problem with JsonResult.Data.

It looks in debuger like that: jsonResult.Data { success = false, message = "some string" } it's annonymous type

-> success : false : bool -> message : "some string" : string

So I cast data to dynamic to do that: dynamic jsonData = jsonResult.Data;

and in debugger it shows that jsonData have property success and message.

Assert.False(jsonData.success) // throws exception RuntimeBinderException about that jsonData do not have success property.

But when I have collection in jsonData I can do this: jsonData.Data[0].success // thats work.

Anyone can help?

Code: SomeMethod retuns

return Json(new { success = false, message = "Some string" });

var jsonResult = this._controller.SomeMethod(null);
dynamic jsonData = jsonResult.Data;
Assert.NotNull(jsonResult);
Assert.False(jsonData.success);
Nerf
  • 938
  • 1
  • 13
  • 30
  • Please show actual code which demonstrates the problem. – nodots Jul 15 '15 at 11:29
  • This anonymous type may be defined in a different assembly from the one you are accessing it. See this question and answers: http://stackoverflow.com/questions/30187883/listdynamic-elements-have-fields-but-i-cannot-access-them-why – Veverke Jul 15 '15 at 11:30
  • I have added that line in assembly file. – Nerf Jul 15 '15 at 11:33
  • Why use anonymous types or dynamic at all? Why not just define a formal contract class? – GeirGrusom Jul 15 '15 at 11:37
  • @GeirGrusom: That was the very question I was asked in my thread, to which my answer was "project constraints/requirements". I agree that, unless there is no real good excuse/reason for it (can't think of one, actually), we should not be returning anonymous types freely. – Veverke Jul 15 '15 at 11:38
  • 1
    @Nerf: so adding InternalsVisibleToAttribute to the assembly where the anonymous type is defined does not help ? – Veverke Jul 15 '15 at 11:42
  • To assembly file from project where is tests? Yes it didnt. – Nerf Jul 15 '15 at 11:47
  • You have to add it to the project that contains the anonymous types. – GeirGrusom Jul 15 '15 at 11:51
  • @Nerf what I meant was that you don't necessarily have to return anonymous types. You can use `JObject` or define a formal contract instead by creating a *public* class that contains the properties of the JSON response. In the last case you won't need dynamic, and you'll get intellisense and the response is suddenly documented properly. – GeirGrusom Jul 15 '15 at 11:54
  • 1
    @Nerf: I meant what GeirGrusom says, you need to add it to the assembly where the anonymous type is defined. This is the assembly that has to make its "internals" visible to others. Hence, the name "InternalsVisibleTo". Pay attention you need to add the assembly to which the internals should be visible, within parenthesis. – Veverke Jul 15 '15 at 11:54
  • I don't have access to other modules of project only to project with unit tests where with I working. – Nerf Jul 15 '15 at 11:57
  • What about this: `dynamic jsonData = new JObject(jsonResult.Data);` – GeirGrusom Jul 15 '15 at 12:03
  • It throwing an Argument Exception: Could not determine JSON object type for type <>f__AnonymousType2`2[System.Boolean,System.String]. – Nerf Jul 15 '15 at 12:07
  • Well, then you are left with reflection I guess :P – GeirGrusom Jul 15 '15 at 12:50

0 Answers0