Git Product home page Git Product logo

Comments (7)

crookse avatar crookse commented on June 14, 2024 1

@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.

crookse avatar crookse commented on June 14, 2024

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.

RogierdeRuijter avatar RogierdeRuijter commented on June 14, 2024

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.

RogierdeRuijter avatar RogierdeRuijter commented on June 14, 2024

I forgot to add this, but I want to test the endpointMethod so mocking the entire thing isn't really an option :)

from rhum.

ebebbington avatar ebebbington commented on June 14, 2024

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.

crookse avatar crookse commented on June 14, 2024

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:

fake_database.zip

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.

crookse avatar crookse commented on June 14, 2024

hi @RogierdeRuijter, closing this. feel free to re-open.

from rhum.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.