unit-testing - Jest mock /监视 Mongoose 链式(查找、排序、限制、跳过)方法

标签 unit-testing mongoose mocking jestjs chained

我想要做什么:

  • 监视链接到 find() 上的方法调用在静态模型方法定义中使用
  • 链式方法:sort() , limit() , skip()

  • 示例调用
  • 目标:监视传递给静态模型方法定义中每个方法的参数:

    ... 静态方法定义

    const 结果 = await this.find({}).sort({}).limit().skip();

    ... 静态方法定义
  • 做了什么find()作为参数接收:完成 findSpy
  • 做了什么sort()作为参数接收:不完整
  • 做了什么limit()作为参数接收:不完整
  • 做了什么skip()作为参数接收:不完整

  • 我尝试过的:
  • mockingoose库,但仅限于 find()
  • 我已经能够成功模拟 find()方法本身,而不是它之后的链式调用
  • const findSpy = jest.spyOn(models.ModelName, 'find');
  • 研究模拟链式方法调用但没有成功
  • 最佳答案

    我无法在任何地方找到解决方案。这是我最终解决这个问题的方法。 YMMV,如果您知道更好的方法,请告诉我!

    给出一些上下文,这是 REST implementation of the Medium.com API 的一部分我正在作为一个副项目工作。

    我是如何 mock 他们的

  • 我模拟了每个链接方法,并设计为返回模型模拟对象本身,以便它可以访问链中的下一个方法。
  • 链中的最后一个方法 (skip) 旨在返回结果。
  • 在测试本身中,我使用了 Jest mockImplementation()为每个测试设计其行为的方法
  • 然后可以使用 expect(StoryMock.chainedMethod).toBeCalled[With]() 监视所有这些。

  • const StoryMock = {
      getLatestStories, // to be tested
      addPagination: jest.fn(), // already tested, can mock
      find: jest.fn(() => StoryMock),
      sort: jest.fn(() => StoryMock),
      limit: jest.fn(() => StoryMock),
      skip: jest.fn(() => []),
    };
    

    要测试的静态方法定义

    /**
     * Gets the latest published stories
     * - uses limit, currentPage pagination
     * - sorted by descending order of publish date
     * @param {object} paginationQuery pagination query string params
     * @param {number} paginationQuery.limit [10] pagination limit
     * @param {number} paginationQuery.currentPage [0] pagination current page
     * @returns {object} { stories, pagination } paginated output using Story.addPagination
     */
    async function getLatestStories(paginationQuery) {
      const { limit = 10, currentPage = 0 } = paginationQuery;
    
      // limit to max of 20 results per page
      const limitBy = Math.min(limit, 20);
      const skipBy = limitBy * currentPage;
    
      const latestStories = await this
        .find({ published: true, parent: null }) // only published stories
        .sort({ publishedAt: -1 }) // publish date descending
        .limit(limitBy)
        .skip(skipBy);
    
      const stories = await Promise.all(latestStories.map(story => story.toResponseShape()));
    
      return this.addPagination({ output: { stories }, limit: limitBy, currentPage });
    }
    

    完整的 Jest 测试以查看模拟的实现

    const { mocks } = require('../../../../test-utils');
    const { getLatestStories } = require('../story-static-queries');
    
    const StoryMock = {
      getLatestStories, // to be tested
      addPagination: jest.fn(), // already tested, can mock
      find: jest.fn(() => StoryMock),
      sort: jest.fn(() => StoryMock),
      limit: jest.fn(() => StoryMock),
      skip: jest.fn(() => []),
    };
    
    const storyInstanceMock = (options) => Object.assign(
      mocks.storyMock({ ...options }),
      { toResponseShape() { return this; } }, // already tested, can mock
    ); 
    
    describe('Story static query methods', () => {
      describe('getLatestStories(): gets the latest published stories', () => {
        const stories = Array(20).fill().map(() => storyInstanceMock({}));
    
        describe('no query pagination params: uses default values for limit and currentPage', () => {
          const defaultLimit = 10;
          const defaultCurrentPage = 0;
          const expectedStories = stories.slice(0, defaultLimit);
    
          // define the return value at end of query chain
          StoryMock.skip.mockImplementation(() => expectedStories);
          // spy on the Story instance toResponseShape() to ensure it is called
          const storyToResponseShapeSpy = jest.spyOn(stories[0], 'toResponseShape');
    
          beforeAll(() => StoryMock.getLatestStories({}));
          afterAll(() => jest.clearAllMocks());
    
          test('calls find() for only published stories: { published: true, parent: null }', () => {
            expect(StoryMock.find).toHaveBeenCalledWith({ published: true, parent: null });
          });
    
          test('calls sort() to sort in descending publishedAt order: { publishedAt: -1 }', () => {
            expect(StoryMock.sort).toHaveBeenCalledWith({ publishedAt: -1 });
          });
    
          test(`calls limit() using default limit: ${defaultLimit}`, () => {
            expect(StoryMock.limit).toHaveBeenCalledWith(defaultLimit);
          });
    
          test(`calls skip() using <default limit * default currentPage>: ${defaultLimit * defaultCurrentPage}`, () => {
            expect(StoryMock.skip).toHaveBeenCalledWith(defaultLimit * defaultCurrentPage);
          });
    
          test('calls toResponseShape() on each Story instance found', () => {
            expect(storyToResponseShapeSpy).toHaveBeenCalled();
          });
    
          test(`calls static addPagination() method with the first ${defaultLimit} stories result: { output: { stories }, limit: ${defaultLimit}, currentPage: ${defaultCurrentPage} }`, () => {
            expect(StoryMock.addPagination).toHaveBeenCalledWith({
              output: { stories: expectedStories },
              limit: defaultLimit,
              currentPage: defaultCurrentPage,
            });
          });
        });
    
        describe('with query pagination params', () => {
          afterEach(() => jest.clearAllMocks());
    
          test('executes the previously tested behavior using query param values: { limit: 5, currentPage: 2 }', async () => {
            const limit = 5;
            const currentPage = 2;
            const storyToResponseShapeSpy = jest.spyOn(stories[0], 'toResponseShape');
            const expectedStories = stories.slice(0, limit);
    
            StoryMock.skip.mockImplementation(() => expectedStories);
    
            await StoryMock.getLatestStories({ limit, currentPage });
            expect(StoryMock.find).toHaveBeenCalledWith({ published: true, parent: null });
            expect(StoryMock.sort).toHaveBeenCalledWith({ publishedAt: -1 });
            expect(StoryMock.limit).toHaveBeenCalledWith(limit);
            expect(StoryMock.skip).toHaveBeenCalledWith(limit * currentPage);
            expect(storyToResponseShapeSpy).toHaveBeenCalled();
            expect(StoryMock.addPagination).toHaveBeenCalledWith({
              limit,
              currentPage,
              output: { stories: expectedStories },
            });
          });
    
          test('limit value of 500 passed: enforces maximum value of 20 instead', async () => {
            const limit = 500;
            const maxLimit = 20;
            const currentPage = 2;
            StoryMock.skip.mockImplementation(() => stories.slice(0, maxLimit));
    
            await StoryMock.getLatestStories({ limit, currentPage });
            expect(StoryMock.limit).toHaveBeenCalledWith(maxLimit);
            expect(StoryMock.addPagination).toHaveBeenCalledWith({
              limit: maxLimit,
              currentPage,
              output: { stories: stories.slice(0, maxLimit) },
            });
          });
        });
      });
    });
    

    关于unit-testing - Jest mock /监视 Mongoose 链式(查找、排序、限制、跳过)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54561550/

    相关文章:

    angular - ng2-translate:无法读取 TranslatePipe.transform 处未定义的属性 'subscribe'

    Xcode 6 测试目标问题

    ios - 如何在 iOS 上对不应该公开的功能进行单元测试?

    javascript - Mongoose,用一组ObjectID查询一个集合

    node.js - 如何计算 Mongoose/NodeJS 中时间戳字段中不同日期项的数量?

    安卓 Espresso 。 perform(click()) 等待通过单击按钮启动的任务完成

    java - 在java中测试如何检查接口(interface)的方法是否被调用?

    java - 使用 Mockito 模拟静态字段

    c# - 模拟 Linq2Sql DataContext

    mongodb - InsertMany 在 mongodb 中不起作用