I want to write unit tests on some js that uses PouchDB library. In order to use PouchDB, I need to start a simple HTTP server*.

My file structure is /public (all the html and js that is served) /specs (all of my js and ruby spec files)

If I were to start a server (like rackup) in the /public folder, I wouldn't be able to access the specs in the /specs folder (because the /public folder would become the root of the web server).

If I were to start rackup in the /specs folder, I wouldn't be able to access the js files I want to test in the /public folder (because the /specs folder would be come the root of the web server).

I guess if I restructured it so that my specs were in my public folder, this could work. But this seems pretty clunky.I guess I could also duplicate the js files I am testing inside the specs folder. Again, this seems pretty clunky. Surely, there is a better way to do this.

*When I try to do the following simple code without a simple http server running, I get an error:

var db = new PouchDB('todos');
db.put({_id : '001' });

I get:

The request is in state "Rejected" with an "outcome=TypeError: invalid 'in' operand i"

Attempting the same code with an http server using "python -m SimpleHTTPServer" will work okay (requests will be fulfilled).

有帮助吗?

解决方案

In order to use PouchDB, I need to start a simple HTTP server.

I'm not sure how you're using PouchDB (on Node? in the browser? PouchDB Server?), but you don't need an HTTP server to run it. You can actually swap in and out any backend you want: CouchDB, LevelDB, IndexedDB, or WebSQL.

There's also an in-memory backend you can use in Node, and we're working on making it a browser plugin too, so that should be ideal for testing.

E.g. here's your HTTP-backed Pouch:

var db = new PouchDB('http://localhost:5984/mydb');

And here's your local Pouch:

var db = new PouchDB('myLocalDb');

And here's your in-memory Pouch (only works in Node right now):

var db = new PouchDB('myInMemoryDb', {db: require('memdown')});

Does that help?

其他提示

I ended up doing it a semi-clunky way. I created a sym link to the resources I wanted in the public folder to inject them into the specs. I could then run a server inside the spec to test pouchDB correctly

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top