Setting up Juggling DB and using mocha for tests

15 views
Skip to first unread message

Gelnior

unread,
Apr 16, 2012, 6:10:04 AM4/16/12
to rail...@googlegroups.com
Hi,

 I have two questions :

- Yesterday I upgraded Railway to last version available in npm registry. Since that, my railway apps do not work anymore : my models (schema) are undefined. I tried to add a require line on top of file, but now it says that "define" function is undefined. I read in the mailing list that now JugglingDB is not the default ORM. So here is my question: what I have to do to get back previous behavior ?

- I prefer using Mocha to write my tests but when I use it, I'm not in my Railway environment, models are not accessible. I mostly do test through HTTP requests but I would like to do some DB cleaning and tests through Railways models. What should I do to have access to my Railway code from my Mocha tests ?
NB : The second problem is not linked to upgrading.

Regards,

Frank

Anatoliy Chakkaev

unread,
Apr 16, 2012, 8:20:39 AM4/16/12
to rail...@googlegroups.com
Hi!

1. add jugglingdb to your project dependencies (in package.json), and add line "require('jugglingbd');" to your npmfile.js
2. you have to load railway env in your tests (just require your 'server.js' for example)
--
Thanks,
Anatoliy Chakkaev

Andreas

unread,
Apr 16, 2012, 1:35:44 PM4/16/12
to rail...@googlegroups.com
Hello Frank,

I'm currently building all my tests with mocha, but mostly through HTTP like you.
But just for testing and exploring, I created a dummy before() action in my test case and my models are directly accessible.

Maybe my code is a bit different. I will post it here, maybe it helps you:

var app     = require('../server'),
    tobi    = require('tobi'),
    browser = tobi.createBrowser(3001, '127.0.0.1');

app.listen(3001)

describe('GET /books *', function() {
   
    // dummy before action to test accessibility of defined models
    before(function(done) {
        // also Book.all() works

        app.models.Book.all({where: {name: 'Game of Throne'}}, function(err, result) {
            if(!result || (result && result.length == 0)) {
                Book.find(1, function(err, b) {
                    b.updateAttribute('name', 'Game of Throne', function(err, result) {
                        done()
                    });
                })
            }
        });
    });

    it('should validate the page elements', function(done) {
        browser.get('/books', function(res, $){
            res.should.have.status(200);
            $('title').should.have.text('Books:Books index');
            $('h1').should.have.text('My Books');
            $('a.btn.btn-primary').should.have.text(' Add Book');
            // hint: use css not() selector because it would match the delete button too
            $('a[href="/books/1"]:not([data-remote])').should.have.text('Game of Thrones');
           
            done();
        });
    });
});
/ end code example

Regards,
Andreas

Gelnior

unread,
Apr 17, 2012, 9:54:27 AM4/17/12
to rail...@googlegroups.com
Thanks guys, everything works fine now.

Should we put Andreas snippet in the railway wiki ?

Frank

Anatoliy Chakkaev

unread,
Apr 17, 2012, 10:31:43 AM4/17/12
to rail...@googlegroups.com
I think we can if Andreas don't mind. Just clean it up.
--
Thanks,
Anatoliy Chakkaev

Andreas

unread,
Apr 17, 2012, 1:13:53 PM4/17/12
to rail...@googlegroups.com
Of course you can use it in the wiki.

Gelnior

unread,
Apr 17, 2012, 4:18:16 PM4/17/12
to rail...@googlegroups.com
I added it to the snippet section. Feel free to change it if I did wrong.

https://github.com/1602/express-on-railway/wiki/Snippets
Reply all
Reply to author
Forward
0 new messages