sequelize-mock icon indicating copy to clipboard operation
sequelize-mock copied to clipboard

Document setting up mock models with multiple rows

Open jathanasiou opened this issue 7 years ago • 5 comments

Currently the examples are very limited and it is not clear how one could have multiple rows in their mock data. Neither the readme nor the documentation seem to cover how that would be possible.

For example the below example only describes the usage of a single row

var UserMock = DBConnectionMock.define('users', {
		'email': '[email protected]',
		'username': 'blink',
		'picture': 'user-picture.jpg',
	}, {
		instanceMethods: {
			myTestFunc: function () {
				return 'Test User';
			},
		},
	});

Whereas directly below we have a WHERE example which is completely unclear on how to actually reproduce. This is because there is neither information on adding multiple rows, nor is the example condition and fields valid for the above row.

UserMock.findOne({
	where: {
		username: 'my-user',
	},
}).then(function (user) {
	// `user` is a Sequelize Model-like object
	user.get('id');         // Auto-Incrementing ID available on all Models
	user.get('email');      // '[email protected]'; Pulled from default values
	user.get('username');   // 'my-user'; Pulled from the `where` in the query
	
	user.myTestFunc();      // Will return 'Test User' as defined above
});

jathanasiou avatar Dec 13 '17 11:12 jathanasiou

I met the same problem when init model with multiple records. I have tried to pass the data as an array, but the result from query was wrong. Does any way solve this?

davidnguyen11 avatar Mar 01 '18 13:03 davidnguyen11

Any updates on this? I can't use this lib without ability to define a collections of mock instances.... *( The only thing I found so far is $queueResult. But this does not seems like a solution for me since I need mock data to be returned on each findAll call, not only on next

SET001 avatar Jan 02 '19 14:01 SET001

Any solutions for this issue?

ViniciusTavares avatar Jan 10 '19 00:01 ViniciusTavares

I am facing the same problem. hope to get it fixed soon.

surajrahel avatar Apr 07 '19 13:04 surajrahel

Took me a few hours to find but I found $queueResult aswell. My implementation for my test was like this:

jest.mock('./../../models/my_news_model', () => () => {
  var SequelizeMock = require('sequelize-mock');
  var dbMock = new SequelizeMock();
  var NewsModel = dbMock.define('news_model');
  NewsModel.$queueResult([
    NewsModel.build({
      id: 1,
      title: 'The Test Title',
      description: 'This is the news story',
      basemedia: "5cf5069bd8ad580bf03728a7.jpg",
      link: 'https://newsfeed.com/somestory',
      createdAt: '2019-06-03 13:13:45',
      updatedAt: '2019-06-03 13:13:45'
    }),
    NewsModel.build({
      id: 2,
      title: 'The Test Title 2',
      description: 'This is the news story 2',
      basemedia: "5cf5069bd8ad580bf03728a72.jpg",
      link: 'https://newsfeed.com/somestory2',
      createdAt: '2019-06-02 13:13:45',
      updatedAt: '2019-06-02 13:13:45'
    })
  ]);
  return NewsModel;
});

There are still many unanswered questions, and this been the plug and play package I was hoping for is not the case

andrewsteadcc avatar Jun 04 '19 16:06 andrewsteadcc