How can I test database queries without creating mocks - javascript

I am using ORM like Sequelize to interact with the database but I want to test the queries as unit test how can I do that without creating any fake models or instant methods like using sequelize mock.

If you don't want to mock then don't do it!
Use unit tests to test your database with the real queries - you will most likely need some "setup" and "teardown" scripts/help methods.
If you are new to unit testing, start here: https://martinfowler.com/bliki/UnitTest.html

Related

MongoDB create DB for tests

Is there a way to create a proxy database for server(Node.js) tests (Mocha+ Chai). DB for development and production are created on MLab. But idea create one more DB especially for the tests seems to me not so good idea.
I agree with Thilo's answer that having a dedicated DB for tests is a good idea, but you can also use something like mongo-mock for this, which is an in-memory MongoDB instance you can use for tests.

Writing mocks for arangodb

I was wondering if there is something that I can use to mock my database. Say a function makeRelations makes relation from a certain node to another. I want to test this function but without actually making a relation in my database. Is there an easy way to do so ?
I am using expect and mocha for testing.
To access database (I don't have experience with arangodb), you typically use some driver library. You can fake that driver library calls with proxyquire.
You're testing what is likely already tested at some level.

Ember clear records between acceptance tests using Local Storage adapter

I am writing some acceptance tests for my Ember app and I don't know the best way to remove records created in localStorage between tests. I also don't want to lose any localStorage records I have created in my development environment.
I am currently setting a namespace in the LSAdapter using an environment variable so that development and test records are stored separately:
export default DS.LSAdapter.extend({
namespace: ENV.taskLocalStorageKey
});
I can then remove the key in the afterEach hook in my tests: localStorage.removeItem(ENV.taskLocalStorageKey); but I am sure there is much better way to do this? I am also using ember data factory guy which I thought would do this for me automatically (when I try to do a TestHelper.handleCreate('task-local'); I get an error adapter.buildURL is not a function).

Testing services along with controller and API routes

I currently use jasmine with karma to unit test my Angular controllers. I use JSON fixtures with mock data rather than relying on my actual services to make API calls.
Separately I test my API endpoints using jasmine-node.
My question is, do I need to also test my services in order to have a solid test suite? I feel like it would be overkill as all that is in my services are very simple $resource GET methods, and testing $resource is obviously not something which is necessary.

How to avoid partial mocking or how to do full mocks using sinon?

As far as I know all mocks in SinonJS are partial mocks, witch lead to uncut dependencies in tests when doing TDD.
This is an example of testing a method that connects to database and performs an insert:
Fist test: Mock connection to database to check params.
Implement connection logic to pass test.
Second test: Stub connection to database and mock insert to database.
Implement insert logic to pass test.
Doing this the first test will do the insert logic.
I know that full instances can be stubbed, but once stubbed it cannot be mocked.
Is there any way to create a full mock or any alternative?

Categories