Jasmine spyOn function call inside function under test - jasmine

I am trying to use Jasmine to test the following function:
var Pdba = Class.create();
Pdba.prototype = {
getChangeGroup: function(userId) {
var query = 'active=true^u_change_group=true^u_organization=false^';
var exGroup = new CompanyGroup();
var groups = exGroup.getGroupsByQuery(userId, query); //want to spy/mock this call
if (groups.next()) {
return groups.sys_id.toString();
}
return '';
}
type: 'Pdba'
};
I want to SpyOn the getGroupsByQuery() call, so that it doesn't make the actual call. Below is a collection of various things I have been trying, mostly just to see if I can "spy" and see that it has been called, then work on overriding so that I can replace the call with my own data.
describe('my suite of getChangeGroup tests', function() {
var expPdba;
var validUserId = 'user1';
var expGrp;
var ggbqMoc
beforeEach(function() {
expPdba = new global.Pdba();
coGrp = new CompanyGroup();
spyOn(coGrp, 'getGroupsByQuery');
ggbqMoc = jasmine.createSpy('getGroupsByQuery');
});
it('should return \'\' for empty userId', function() {
coPdba.getChangeGroup('');
expect(coGrp.getGroupsByQuery).toHaveBeenCalled();
expect(ggbqMoc).toHaveBeenCalled();
});
});
Is this possible or do I need to change the function under test to take a 'CompanyGroup' as a parameter?
Thank you

I'm assuming you are using jasmine v3. The syntax for creating a spy is pretty weird now- you have to pass a string that refers to the name of the variable you want to create a spy for, and then you pass an array of function names that should be spied on.
Try this:
describe('my suite of getChangeGroup tests', function() {
var expPdba;
var validUserId = 'user1';
var expGrp;
var spy;
beforeEach(function() {
expPdba = new global.Pdba();
coGrp = new CompanyGroup();
spy = jasmine.createSpyObj('coGrp', ['getGroupsByQuery'])
});
it('should return \'\' for empty userId', function() {
coPdba.getChangeGroup('');
expect(coGrp.getGroupsByQuery).toHaveBeenCalled();
expect(spy).toHaveBeenCalled();
});
});

Related

multiple Async/await JS function

In my controller, the fn function executes return.suucess() before my variables are properly defined. In this case, the ytb object.
The sails.log of "items" is correct, but "ytb" did not return.
I also tried with Promise.then, and .all. Also, with async.each() and removing the await inside this function.
fn: async function (inputs, exits) {
var url = require('url');
var items = await Collection.find({});
_.each(items, async item => {
if (item.youtube_url) {
var ytbUrl = item.youtube_url;
var regexBaseUrlYtb = new RegExp('(https://www.youtube.com/channel/)');
if (regexBaseUrlYtb.test(ytbUrl)) {
var ytbIdChannel = ytbUrl.replace(regexBaseUrlYtb, '');
var ytbChannel = await sails.helpers.youtubeApi(ytbIdChannel);
item.ytb = {};
Object.assign(item.ytb, ytbChannel);
}
}
sails.log('items: ', item)
});
return exits.success({
items
});
}
I expect that the ytb object will also be passed with the object item at the view, but the items object is passed without the ytb object.
Also, in general, I expect the return exits.success() runs only once the function finished
Thanks.

Nativescript - Pass array from home-view-model to home.js

I´m having a hard time understanding how to perform this action(as the title says), and maybe someone could help me understand the process, my code is below:
My home-view-model:
var Observable = require("data/observable").Observable;
var ObservableArray = require("data/observable-array").ObservableArray;
var http = require("http");
function createViewModel() {
http.getJSON("http://myJsonfile").then(function (r) {
var arrNoticias = new ObservableArray(r.data);
return arrNoticias;
}, function (e) {
});
}
exports.createViewModel = createViewModel;
I have done a console.log of the arrNoticias before i have putted it inside a callback function and it returns [object object] etc...and then i have done this:
console.log(arrNoticias.getItem(0).titulo);
and it returns the info i need!.
Then in my home.js file i have this:
var observableModule = require("data/observable")
var ObservableArray = require("data/observable-array").ObservableArray;
var arrNoticias = require('./home-view-model.js');
console.log(arrNoticias.getItem(0).titulo);
and the result in the console is:
TypeError: arrNoticias.getItem is not a function. (In 'arrNoticias.getItem(0)', 'arrNoticias.getItem' is undefined)
My question is, how does this action is perform? passing the data from view-model to the .js file?
Thanks for your time
Regards
As that function send a URL request so probably it's an async function, which is on hold while requesting so that's why you get undefined. Normally, you will want your function that sends a URL request to return a promise. Based on that promise, you will the result as expected after the request is done. So:
function createViewModel() {
return new Promise<>((resolve, reject) => {
http.getJSON("http://myJsonfile").then(function (r) {
var arrNoticias = new ObservableArray(r.data);
resolve(arrNoticias);
}, function(e) {
reject(e);
});
}), (e) => {
console.log(e);
})
}
In home.js:
var homeVM= require('./home-view-model.js');
var arrNoticias;
homeVM.createViewModel().then(function(r) {
arrNoticias = r;
});

mocha stub using sinon in node.js

I would like to know if I am missing anything with regard to sinon.js I have tried using sinon.stub().returns and yields but am unable to get the result. Any pointers would be helpful
I have a module which calls another module that returns the value from the DB
var users = require('/users');
module.exports.getProfileImage = function (req, res) {
var profile = {};
else {
users.findOne("email", req.session.user.email, function (err, user) {
if (err) {
res.status(400).send();
}
else if (!user) {
//return default image
}
else if (user) {
//Do some other logic here
}
});
};
I am using mocha as the testing framework and am also using sinon. The problem that I am facing is when i create a stub of users.findOne to return a value the control does not come to my else if (user) condition.
my unit test case is as follows
describe("Return image of user",function(){
var validRequest = null;
validRequest={
session:{
user:{
email:'testUser#test.com',
role:'Hiring Company'
}
}
};
it("Should return an image from the file if the user is present in db",function(done){
var findOneUserResponse ={
companyName:"xyz",
email:"xyz#abc.com"
};
var findOne = sinon.stub(mongoose.Model, "findOne");
findOne.callsArgWith(1,null,findOneUserResponse);
user.getProfileImage(validRequest,response);
var actualImage = response._getData();
findOne.restore();
done();
};
};
So I went through the sinon.js documentation http://sinonjs.org/docs/ and came across what I was missing
describe("Return image of user",function(){
var validRequest = null;
validRequest={
session:{
user:{
email:'testUser#test.com',
role:'Hiring Company'
}
}
};
it("Should return an image from the file if the user is present in db",function(done){
var findOneUserResponse ={
companyName:"xyz",
email:"xyz#abc.com"
};
var findOne = sinon.stub(mongoose.Model, "findOne",function(err,callback){
callback(null,findOneUserResponse);
)};
user.getProfileImage(validRequest,response);
var actualImage = response._getData();
findOne.restore();
done();
};
};

Jasmine testing, using a constructor in the beforeEach

i am attempting to create a new instance of two classes that i have already written in separate files. when i try to create new instances of them in the beforeEach() section of the test code, the tests return undefined for my newly created objects. however when i create them in each IT section the test run perfectly.
describe("placeStone", function() {
beforeEach(function() {
var go = new Display();
var logic = new Internals(go);
logic.tempBoard = [ array];
});
it("should place a black stone at 0,6", function() {
logic.placeStone(logic.black,0,6);
expect(logic.tempBoard[6][0]).toEqual("B");
});
this returns logic undefined.
describe("placeStone", function() {
it("should place a black stone at 0,6", function() {
var go = new Display();
var logic = new Internals(go);
logic.tempBoard = [ array];
logic.placeStone(logic.black,0,6);
expect(logic.tempBoard[6][0]).toEqual("B");
});
});
this seems to work the way i want. how can i get it to work in the beforeEach() section?
var logic should be defined in the scope of the describe function, then it exists both in the scope of the beforeEach function and the spec (the it function), e.g.
describe('suite', function () {
var myVar;
beforeEach(function(){
myVar = 10;
});
it('checks myVar', function () {
expect(myVar).toEqual(10);
});
});

updating variable after ajax call made in angularjs

I use factory to make database calls in angularjs
var app = angular.module('myApp', []);
app.factory("Service", function ($http) {
var obj = {};
$http.get('test.txt').then(function (data) {
obj.getApiKey = {'Authorization' : data.data};
});
return obj;
});
app.factory("Orders", function ($http, Service) {
var obj = {};
var api_key = Service.getApiKey;
console.log(api_key);
return obj;
});
In the above program, when i call property of Service factory and log the value of api_key = Service.getApiKey; it shows undefined. Probably its because the data is not received from $http call. How to perform the AJAX call synchronously.
var app = angular.module('myApp', []);
app.factory("Service", function ($http) {
var obj = {};
factory.get=function(success,error){
$http.get('test.txt').then(function (data) {
obj.getApiKey = {'Authorization' : data.data};
});
return obj;
}
});
app.factory("Orders", function ($http, Service) {
Service.get(suc,err);
var suc=function()
{
var obj = {};
var api_key = Service.getApiKey;
console.log(api_key);
return obj;
}
});
As noted by Jonathan, it's not best practice you not use synchronous calls with AJAX, instead, you can return a promisse as a funcion and use it later:
var app = angular.module('myApp', []);
app.factory("Service", function ($http) {
var obj = {};
obj.getApiKey = $http.get('test.txt').then(function (data) {
return {'Authorization' : data.data};
});
return obj;
});
app.factory("Orders", function ($http, Service) {
var obj = {};
obj.getApiKey = Service.getApiKey.then(function(data){
console.log(data);
return data;
});
return obj;
});
In this case, the service is returning a promisse object, this way you can ensure values will be called successfully and data will be available after the call.
Return a Promise from your service:
app.factory("Service", function ($http) {
return {
getApiKey: function() {
return $http.get('test.txt');
}
};
});
Inject your service, and use the Promise API to get your data asynchronously:
app.factory("Orders", function ($http, Service) {
var obj = {};
var api_key = {};
Service.getApiKey().then(function(apiKey) {
api_key = apiKey.data;
});
});

Resources