Typeorm装饰器不起作用

时间:2020-04-16 14:52:22

标签: typescript jestjs typeorm ts-jest

我有以下要测试的控制器:

class Album {
  public static getAlbums(): Promise<AlbumModel[]> {
    return getRepository(AlbumModel).find({ relations: ['pictures'] });
  }
}

哪个链接到模型,我正在使用带修饰器的typeorm。这是我使用Jest时出现的问题

import { Entity, PrimaryGeneratedColumn, Column, JoinTable, ManyToMany } from 'typeorm';
import { PicturesModel } from '../pictures/pictures.model';

@Entity({
  name: 'T_ALBUM_AL',
  synchronize: true,
})
export class AlbumModel {
  @PrimaryGeneratedColumn({
    name: 'AL_id',
  })
  id: number;

  @Column({
    name: 'AL_name',
  })
  name: string;

  @ManyToMany(() => PicturesModel, (picture: PicturesModel) => picture.id)
  @JoinTable({
    name: 'TJ_PICTURE_ALBUM_PA',
    joinColumn: {
      name: 'AL_id',
      referencedColumnName: 'id',
    },
    inverseJoinColumn: {
      name: 'PI_id',
      referencedColumnName: 'id',
    },
  })
  pictures: PicturesModel[];
}

要测试此控制器,我正在使用ts-jest。这是我的单元测试:

import Album from './album.controller';

const getRepMock = jest.fn(() => ({
  find: jest.fn().mockImplementation(),
}));

jest.mock('typeorm', () => ({
  getRepository: getRepMock,
}));

describe('Album', () => {
  it('Should work', () => {
    Album.getAlbums();
    expect(getRepMock).toBeCalled();
  });
});

运行测试时,出现以下错误:

 FAIL  src/api/v1/album/album.test.ts
  ● Test suite failed to run

    TypeError: typeorm_1.PrimaryGeneratedColumn is not a function

       6 | })
       7 | export class PicturesModel {
    >  8 |   @PrimaryGeneratedColumn({
         |    ^
       9 |     name: 'PI_id',
      10 |   })
      11 |   id: number;

这有什么问题?

这是我package.json的一部分

  "jest": {
    "preset": "ts-jest",
    "testEnvironment": "node",
    "coveragePathIgnorePatterns": [
      "/node_modules/"
    ]
  },
  "dependencies": {
    "@types/dotenv": "^8.2.0",
    "body-parser": "^1.19.0",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "express-boom": "^3.0.0",
    "glob": "^7.1.6",
    "morgan": "^1.10.0",
    "mysql": "^2.14.1",
    "pg": "^7.18.2",
    "reflect-metadata": "^0.1.10",
    "ts-jest": "^25.3.1",
    "typeorm": "0.2.24"
  },
  "devDependencies": {
    "@types/express": "^4.17.3",
    "@types/express-boom": "^3.0.0",
    "@types/glob": "^7.1.1",
    "@types/jest": "^25.2.1",
    "@types/morgan": "^1.9.0",
    "@types/node": "^8.0.29",
    "@types/supertest": "^2.0.8",
    "@typescript-eslint/eslint-plugin": "^2.24.0",
    "@typescript-eslint/parser": "^2.24.0",
    "eslint": "^6.8.0",
    "eslint-config-airbnb-base": "^14.1.0",
    "eslint-import-resolver-alias": "^1.1.2",
    "eslint-plugin-import": "^2.20.1",
    "eslint-plugin-module-resolver": "^0.16.0",
    "jest": "^25.3.0",
    "nodemon": "^2.0.2",
    "supertest": "^4.0.2",
    "ts-node": "3.3.0",
    "typescript": "3.3.3333",
    "typescript-eslint": "^0.0.1-alpha.0"
  }

1 个答案:

答案 0 :(得分:0)

您需要像这样模拟import random from collections import Counter c = Counter() for _ in range(100000): c[random.randrange(101)] += 1 result = dict(sorted(c.items())) # >>> print(result) # {0: 936, 1: 1011, 2: 989, 3: 1005, 4: 1036, 5: 992, 6: 1013, 7: 1008, 8: 983, 9: 987, 10: 982, 11: 991, 12: 986, 13: 973, 14: 973, 15: 988, 16: 952, 17: 1025, 18: 958, 19: 989, 20: 984, 21: 934, 22: 997, 23: 960, 24: 989, 25: 1005, 26: 1006, 27: 1019, 28: 1000, 29: 1003, 30: 1007, 31: 993, 32: 996, 33: 993, 34: 1010, 35: 1041, 36: 955, 37: 965, 38: 991, 39: 995, 40: 1036, 41: 967, 42: 1013, 43: 957, 44: 930, 45: 1037, 46: 1031, 47: 1017, 48: 1050, 49: 1005, 50: 954, 51: 968, 52: 1010, 53: 984, 54: 1012, 55: 1017, 56: 980, 57: 928, 58: 976, 59: 968, 60: 970, 61: 1011, 62: 949, 63: 1018, 64: 982, 65: 990, 66: 1011, 67: 985, 68: 1001, 69: 964, 70: 957, 71: 977, 72: 1007, 73: 1028, 74: 1049, 75: 930, 76: 949, 77: 998, 78: 981, 79: 1001, 80: 1000, 81: 976, 82: 1064, 83: 994, 84: 993, 85: 999, 86: 1007, 87: 1021, 88: 958, 89: 957, 90: 989, 91: 990, 92: 990, 93: 992, 94: 1027, 95: 973, 96: 907, 97: 973, 98: 944, 99: 979, 100: 1049}

class User:
    def __init__(self, first_name, last_name, user_name, password, email):
        self.first_name = first_name
        self.last_name = last_name
        self.user_name = user_name
        self.password = password
        self.email = email
    def describe_user(self):
        print(f"{self.first_name}" + " " + "{self.last_name}")
        print(f"Your user_name is {self.user_name}")
        print(f"Your password is {self.password}")
        print(f"Your email is {self.email}")
    def greet_user(self):
        print(f"Hello {self.first_name}")

my_name = User('Eric', "Tekell", "tech1329", "5555", "eric.tekell@yahoo.com")
my_name.describe_user()
my_name.greet_user()

your_name = User("John", "Wayne", "wayne55", "6666", "johnwayne@yahoo.com")
your_name.describe_user()
相关问题