drashland / rhum

A test double library
https://drash.land/rhum
MIT License
92 stars 6 forks source link

Mocked object doesn't process mixin class getter/setter #145

Closed dominikj111 closed 2 years ago

dominikj111 commented 2 years ago

Summary

The getter and setter defined in the mixin class is not processed. The mock see that property as undefined.

Steps To Reproduce The Bug

import { Rhum } from "https://deno.land/x/rhum@v1.1.14/mod.ts";
import { assertInstanceOf } from "https://deno.land/std@0.133.0/testing/asserts.ts";

export interface IGame {}

export class NullGame implements IGame {}

function WithGameProperty() {
  return <T extends new (...args: any[]) => any>(GameProperty: T) => {
    return class extends GameProperty {
      private game: IGame = new NullGame();

      public set Game(game: IGame) {
        this.game = game;
      }

      public get Game() {
        return this.game;
      }
    };
  };
}

const PlayersEngine = WithGameProperty()(
  class PlayersEngine {
    onNextGenerationReady(listener: () => void): void {
      // add listener to react on an event
    }

    prepareNextGeneration(): void {
      // do some stuff and trigger an event
    }
  },
);

const pe = new PlayersEngine();
const mpe = Rhum.mock(PlayersEngine).create();

assertInstanceOf(pe.Game, NullGame);
assertInstanceOf(mpe.Game, NullGame);

Expected Behavior

It will pass last line in the code above without an error.

ebebbington commented 2 years ago

Hello @domino2, I’m assuming you’re the one who posted on SO

Apologies that you’re still having issues with this, we’ll look into it :)

dominikj111 commented 2 years ago

Yeah ... this is another issue actually (just related) :) I'm looking forward.

ebebbington commented 2 years ago

thanks again :) this does seem to be working as expected though

Game is a getter/setter, what i think you want to be checking is game as thats the property with a type of NullGame

Though there will be a problem with the type hinting on this, mock.game will throw a ts error, so i will get back to you on passing a generic to the mock call

ebebbington commented 2 years ago

Oh i see your problem actally, game is initiased, so pa.Game instanceof NullGame should be true

ebebbington commented 2 years ago

@domino2 i've found a fix, i will release a new version soon

crookse commented 2 years ago

hey @domino2 ! i just released v1.1.15 that includes the fix @ebebbington made for this issue. you should be able to bump your rhum version and be good to go:

import { Rhum } from "https://deno.land/x/rhum@v1.1.15/mod.ts";
// or export { Rhum } from "https://deno.land/x/rhum@v1.1.15/mod.ts";

let us know if you have any issues. thanks !