marmelab / FakeRest

Patch fetch/XMLHttpRequest to fake a REST API server in the browser, based on JSON data.
MIT License
439 stars 48 forks source link

Example code doesn't work #21

Closed jojoyuji closed 6 years ago

jojoyuji commented 8 years ago
<script src="/path/to/FakeRest.min.js"></script>
<script src="/path/to/sinon.js"></script>
<script type="text/javascript">
var data = {
    'authors': [
        { id: 0, first_name: 'Leo', last_name: 'Tolstoi' },
        { id: 1, first_name: 'Jane', last_name: 'Austen' }
    ],
    'books': [
        { id: 0, author_id: 0, title: 'Anna Karenina' },
        { id: 1, author_id: 0, title: 'War and Peace' },
        { id: 2, author_id: 1, title: 'Pride and Prejudice' },
        { id: 3, author_id: 1, title: 'Sense and Sensibility' }
    ],
    'settings': {
        language: 'english',
        preferred_format: 'hardback',
    }
};
// initialize fake REST server
var restServer = new FakeRest.Server();
restServer.init(data);
// use sinon.js to monkey-patch XmlHttpRequest
var server = sinon.fakeServer.create();
server.respondWith(restServer.getHandler());
</script>

throws "FakeRest.min.js:1 Uncaught Error: Can't initialize a Collection with anything else than an array of items"

using via bower and sinon installed via: devDependencies: { "sinon-server": "http://sinonjs.org/releases/sinon-server-1.14.1.js" }

blocknotes commented 8 years ago

I got similar problems, try directly with "http://sinonjs.org/releases/sinon-1.17.3.js"

jojoyuji commented 8 years ago

Tried using directly as @blocknotes suggested, still the same error :(

billgloff commented 8 years ago

@jojoyuji It's complaining because 'settings' is not an array so if you just delete that part (or turn it into a proper array of objects), it'll start working. You probably got past this already but maybe it will help someone else looking at this for the first time.

jbaranski commented 6 years ago

I was going to fix this but it doesn't appear to be an issue anymore. Using a plain settings object no error is thrown.

jojoyuji commented 6 years ago

haha, I endup using dyson.js, well thx anyway