question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Document setting up mock models with multiple rows

See original GitHub issue

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@example.com',
		'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@example.com'; 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
});

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:8
  • Comments:5

github_iconTop GitHub Comments

12reactions
andrewsteadcccommented, Jun 4, 2019

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

2reactions
davidnguyen11commented, Mar 1, 2018

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create a Model and Mock Instances | by Tom Brodhurst-Hill
Broadly speaking, an app has two facets: model and view. ... In Xcode, use the File menu's New File… command to create a...
Read more >
Stubbing and Mocking with Mockito and JUnit - Semaphore CI
Learn how to create true unit tests by mocking all external dependencies in your JUnit classes with the help of Mockito.
Read more >
A Unit Testing Practitioner's Guide to Everyday Mockito - Toptal
With Mockito, you create a mock, tell Mockito what to do when specific methods are ... You can also specify multiple values which...
Read more >
Using Test Data With Mock Services - BlazeMeter Docs
A Service Data Model contains one or more Data Entities. You can create several data entities, each of which can contain, for example,...
Read more >
Pytest with Marking, Mocking, and Fixtures in 10 Minutes
There is more than one way (and more than one Python package) to perform unit tests ... To overcome this, create a configuration...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found