Comments (7)
@RogierdeRuijter, so i tried mocking the way you expect to mock and it looks like we need some extra functionality to support your use case. although im unsure what that functionality would look like -- i'd need to discuss this with the other team members
from rhum.
Hey @RogierdeRuijter! Sorry for the late response. I must've overlooked this in our Discord GitHub feed. Anyways, I think your only option might be to mock the entire endpointMethod
.
I do have one question though. Where is db
being defined? If db
is defined outside of endpointMethod
, then you might be able to use a combination of stubs and mocks to accomplish what you want.
from rhum.
Hee @crookse, thanks for your response!
Yes the database is defined in another file. This file looks something like this:
import { MongoClient } from "https://deno.land/x/[email protected]/mod.ts";
export class DB {
public client: MongoClient;
private dbName: string;
private dbUsername: string;
private dbPassword: string;
private dbUrl: string;
constructor() {
this.dbName = Deno.env.get("DB_NAME") || "";
this.dbUsername = Deno.env.get("DB_USERNAME") || "";
this.dbPassword = Deno.env.get("DB_PASSWORD") || "";
this.dbUrl = Deno.env.get("DB_URL") || "";
this.client = {} as MongoClient;
}
async connect() {
const client = new MongoClient();
await client.connect({
db: this.dbName,
tls: true,
servers: [
{
host: this.dbUrl,
port: 27017,
}
],
credential: {
username: this.dbUsername,
password: this.dbPassword,
db: this.dbName,
mechanism: "SCRAM-SHA-1",
},
});
this.client = client;
}
public getDatebase(databaseName: string) {
return this.client.database(databaseName);
}
}
const db = new DB();
export default db;
I was looking at the stubs and mocks documentation. And it doesn't seems to be the case that if I mock the db
like this
Rhum.testPlan("controller.ts", () => {
Rhum.testSuite("endpointMethod()", () => {
Rhum.testCase("it call the endpoint and return status code 200", () => {
const mock = Rhum.mock(db).create();
const result = endpointMethod();
// This is simplified but you get the idea :D
Rhum.asserts.assertEquals(result, 'status code: 200');
});
});
});
Rhum.run();
That the endpointMethod will use the mock. It seems to be that I have to pass it to the endpointMethod like this.
const mock = Rhum.mock(db).create();
const result = endpointMethod(mock);
But I can't do this. Because I can't pass an additional parameter to the method since I use this method as a callback in a post request like this:
import { Application } from "https://deno.land/x/abc/mod.ts";
import { endpointMethod } from "./controllers.ts";
const app = new Application();
app
.post("/endpoint", endpointMethod)
.start({ port: 3000 });
And if I could pass db here to the endpoint method I would prefer not to do this.
I am looking for something similar like how you can mock in Jest. For example, this:
it('should return the product', async () => {
const expectedProduct = {
id: 1,
name: 'football',
};
const productManager = new ProductManager();
// This is the mocking part
----------------
const mockGetById = jest.fn();
ProductsClient.prototype.getById = mockGetById;
mockGetById.mockReturnValue(Promise.resolve(expectedProduct));
----------------
const result = await productManager.getProductToManage(1);
expect(result.name).toBe('football'); // It passes!
});
source. Here you mock the getById
function without passing the method ProductsClient to the getProductToManage.
Is something like this possible?
from rhum.
I forgot to add this, but I want to test the endpointMethod
so mocking the entire thing isn't really an option :)
from rhum.
yeah i think you might actually have to specifically mock those methods, for example
class Database {
pulic db: Database
public endpointMethod() {
const database = db.database("users")
...
}
}
const db = ... // stub it, eg db = { database: (name: string) => { insertOne: (entry: any) => 1 } }
const database = Rhum.mock(DB).create(db)
await db.endpointMethod();
though the syntax is a bit off, but hopefully you get the gist, i wonder if this helps?
from rhum.
hey @RogierdeRuijter, firstly, apologies for a really late update on this. i'm not sure if you're still using rhum, but we released v2 and what you want to accomplish can be done through rhum's new Fake()
test double and using import maps. while it may not be an elegant solution like jest's auto-mocking feature, it doesn't require anything other than rhum and an import map (which in my opinion can help keep the testing part of your codebase pretty lean). if you're still using rhum and still interested in the solution, it's in the zip file below:
i used most of what you provided in your code blocks (aside from the typings) in hopes that you would be able to easily compare the solution with your code.
when you download the project, you can run the following (assuming you have deno upgraded to v1.20.x):
deno test --allow-env --import-map=tests/mocks.json tests/endpoint_method_test.ts
if you have any questions about the setup, please feel free to reach out!
from rhum.
hi @RogierdeRuijter, closing this. feel free to re-open.
from rhum.
Related Issues (20)
- Mocked Class with Request param (Fetch Request Interface) throws TypeError for undefined method HOT 11
- Rhum.mock doesn't track nested method calls HOT 3
- Mocked object doesn't process mixin class getter/setter HOT 6
- Release dependency version HOT 2
- Suggestion: Make it QUnit API HOT 1
- feat(v2): spies
- chore(v2): clean up mock tests
- chore(v2): DRY fakes and mocks logic
- feat(v2): mock.expects(...).toBeCalledWith(...)
- feat(v2): support node
- chore(v2): update repo after release
- chore: clean up/improve code documentation for doc.deno.land
- test: error classes HOT 1
- feat: mock.toBeCalledWithArgs(...) HOT 1
- feat: mock.toBeCalledWithoutArgs() HOT 1
- chore: After verifying the calls the counts don't decreased HOT 2
- feat: set arg expectations on methods
- feat(fake/mock): allow pre-programming methods with callbacks
- bug: Fakes and Mocks don't work with async methods HOT 1
- bug: Spy not spy good on object parameters HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rhum.