vendredi 29 juillet 2011

REST API Testing with Cucumber

To test an API we are working on, I wanted to document/test the API so that client developers would be able to use theses specification to learn how to use the API and the same documentation would be used to test our API.

After some research, I settled (for now) to use Cucumber, json_spec and cucumber-api-steps.

These allow me to write this kind of test against the API:

Feature: User API
Background:
Given the following users exist:
| id | name | password | email |
| 1 | ben | abcdef | ben@email.com |
| 2 | jon | bcdefg | jon@email.com |
When I sign in as "ben@email.com/abcdef"
And I send and accept JSON

Scenario: GET /users
When I send a GET request for "/users"
Then the response status should be "200"
And the JSON should be:
"""
[
{
"email": "ben@email.com",
"name": "ben"
},
{
"email": "jon@email.com",
"name": "jon"
}
]
"""
And the JSON at "0/name" should be "ben"

Scenario: GET users/1
When I send a GET request for "/users/1"
Then the response status should be "200"
And the JSON should be:
"""
{
"email": "ben@email.com",
"name": "ben"
}
"""
And the JSON at "name" should be "ben"

vendredi 8 juillet 2011

Sproutcore 2.0, Templates and Datastore

With Sproutcore 2.0, there are different ways to use the template views :

- Define a view template in the header of a page, define its View class and instantiate it from the body of the page

- Define a template in the body of the page and have it refer to its View class

Template in the <head> of the page

In the HTML:
<head>

<script type="text/html" data-template-name="account">
MODE:{{mode}}
<h1>{{username}}</h1>
<h1>{{city}}</h1>
</script>
</head>

<body>
<script type="text/html" data-view="App.MyView">
<h1>Hello world! {{title}}</h1>
</script>
<script type="text/html">
{{view App.AccountView}}
</script>

</body>

In the javascript:

App.AccountView = SC.View.extend({
templateName: 'account',
usernameBinding: 'App.accountController.content.username',
cityBinding: 'App.accountController.content.city',
mode: 'DEV'
});

Template in the <body> of the page


In the HTML:

<body>
<script type="text/html" data-view="App.MyView">
<h1>Hello world! {{title}}</h1>
</script>

In the javascript:


App.MyView = SC.View.extend({
mouseDown: function() {
window.alert("hello world!");
},
title: 'HELLO',
});

Adding the datastore


TBD


Source code
app.js

var App = SC.Application.create({
store: SC.Store.create().from(SC.Record.fixtures)
});

App.MyView = SC.View.extend({
mouseDown: function() {
window.alert("hello world!");
},
title: 'HELLO',
});

App.Account = SC.Record.extend({
username: SC.Record.attr(String),
city: SC.Record.attr(String)
});

App.Account.FIXTURES = [ { username: 'bilou', city: 'new york' } ]

App.accountController = SC.Object.create({
content: SC.Object.create({ username: 'TEST', city: 'San Francisco' }),
city: 'SF'
});

App.AccountView = SC.View.extend({
templateName: 'account',
usernameBinding: 'App.accountController.content.username',
cityBinding: 'App.accountController.content.city',
mode: 'DEV'
});

SC.$(document).ready(function(){
var account = App.store.find(App.Account);
App.accountController.content = account.objectAt(0);
});