Custom assertions:

 T.REST uses Json.NET internally for deserializing the JSON returned from your REST ressource. You can take advantage of this to deserialize to your own custom objects and perform your own custom assertions:

[TestMethod]
public void JqueryUIDemo_DeserializeToObject()
{
    var res = RessourceFactory.Create(new RestConfiguration
    {
        Url = "/svn/trunk/demos/json/demo.json",
        Host = "jquery-ui-map.googlecode.com",
        ExpectArrayResult = true
    });

    var request = new ResRequest<GeoLocations>(res);
    var obj = request.Parse((sz, str) => sz.Deserialize<GeoLocations>(str));

    Assert.AreNotEqual(null, obj.Markers);     
    Assert.AreEqual(2, obj.Markers.Count);
}

private class GeoLocations
{
    public List<GeoLocation> Markers { get; set; }
}

private class GeoLocation
{
    public decimal Latitude { get; set; }
    public decimal Longitude { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
}

Execute call against REST API and verify expected change of state

Using T.REST you can execute a call against an API and execute another call verifying the expected result. This example simulates the creation of a newsletter subscription and verifies against the API that the Email is indeed now a registered newsletter subscriber:

 

[TestMethod]
public void SubscribeToNewsletter_VerifyCallExecution()
{
    var res = RessourceFactory.Create(new RestConfiguration
                    {
                        Host = "my.restapi.net",
                        Url = "/newsletter/subscribe",
                        Method = "POST",
                        FormsCollection = new NameValueCollection
                                            {
                                                {"name", "John Doe"},
                                                {"email", "john@doe.com"}
                                            },                                                      
                    });
    var request = new ResRequest(res);
    request.Execute();

    var expectedResult = new Dictionary<string, Type>
                                {
                                    {"email", typeof (string)},
                                    {"subscribed", typeof (bool)}
                                };
    res = RessourceFactory.Create(new RestConfiguration
    {
        Host = "my.restapi.net",
        Url = "/newsletter/subscriptions/john%40doe.com",
        ExpectedObjectSignature = expectedResult
    });
    res.ValidateSignature();
}

 

Last edited Oct 17, 2012 at 9:17 PM by kerbou, version 5

Comments

No comments yet.