Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7942121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:52:17+00:00 2026-06-03T23:52:17+00:00

I have the following NancyFX unit test. I use the Shouldly assertion library to

  • 0

I have the following NancyFX unit test. I use the Shouldly assertion library to give the set of extensions methods that start .Should---

[Fact]
public void Assessment__Should_return_assessment_state_for_specified_user()
{
    const AssessmentState assessmentState = AssessmentState.Passed;
    var user = Fake.Mentor();

    using (var db = Fake.Db())
    {
        db.Save(user);
        Fake.Assessment(user.Id, db, assessmentState);
        db.ClearStaleIndexes();
    }

    var response = Fake.Browser(user.UserName, user.Password)
            .Get("/assessment/state/" + user.Id, with => with.HttpRequest());

    //var result = (dynamic)body.DeserializeJson<ExpandoObject>();
    var result = (dynamic) JsonConvert.DeserializeObject<ExpandoObject>(response.Body.AsString());

    result.ShouldNotBe(null);
    ((AssessmentState) result.State).ShouldBe(assessmentState);
}

This test calls a AssessmentService uri defined as /assessment/state/" + user.Id which returns a simple JSON object definition that has a single property State of type (enum) AssessmentState, either Passed, Failed or NotStarted.

Here is the service handler so you can see there are no tricks.

Get["/assessment/state/{userid}"] = parameters =>
    {
        var assessment = AssessmentService.GetByUserId(Db, (string)parameters.userid);
        return assessment == null ? HttpStatusCode.NotFound : Response.AsJson(new
                                                                        {
                                                                            assessment.State
                                                                        });
    };

And here is an example the JSON this service call returns:

{"State":1}

Everything works fine until I try to Deserialize the JSON returned by the fake Nancy browser. First I tried to use the built in method provided by Nancy’s BrowserResponse.Body object:

var result = (dynamic)response.Body.DeserializeJson<ExpandoObject>();

This deserializes to an empty object. Which is no good. However, if we use the Newtonsoft equivalent then everything is fine (almost).

var result = (dynamic) JsonConvert.DeserializeObject<ExpandoObject>(response.Body.AsString());

The JSON deserialization now works and so the following Shouldly assertion passes with flying colours:

((AssessmentState) result.State).ShouldBe(assessmentState);

However, for reasons that I suspect have to do with anonymous types, the following line fails at run-time (it compiles fine).

result.ShouldNotBe(null);

Shouldly Exception

That is quite a lot of information. Let me distil it down to two questions:

  1. Why does Nancy’s built in JSON deserializer not work given that the Newtonsoft version does?

  2. How do I work with the dynamic types generated by the JSON de-serialisation so that the Shouldly extension methods do not cause a run-time exception?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:52:19+00:00Added an answer on June 3, 2026 at 11:52 pm

    I can’t answer the first question, but WRT Shouldly and dynamic types, Shouldly’s ShouldNotBe method is an extension method on object. The DLR doesn’t allow you to call extension methods on objects typed as dynamic (hence the runtime binder exception you’re seeing)

    I’d suggest that if you want to call ShouldNotBe(null) on result, you’d have to cast it to an object first (ie: ((object)result).ShouldNotBe(null))

    -x

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following seed input data: Payperiod start: Dec 3rd Payperiod end: Dec 16
I have following entities: public class Product { [Key] public int Id{get;set;} //other properties
I have following code that creates Linq query. I've never used Linq until today
I have following scenario. Some people use approach 1 and some use approach 2.
I have following code to set the font for setting the telugu font. while
I have following classes: Container , Element and then couple of classes that inherit
I have following batch file code: @echo off SET INSTALL_PATH=c:\program files\ :ask_again if exist
I have following link in a.aspx file: <a href=a.htm target=iframe>A</a> I want to set
I have following example string that needs to be filtered 0173556677 (Alice), 017545454 (Bob)
Have following error: skydos@skydos-laptop:/$ service mysql start start: Rejected send message, 1 matched rules;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.