Here is my js code
launchTask(taskId)
{
const taskIds = window.external.MyObjectFactory("INDEXED");
taskIds.add(taskId);
}
And here is how i am trying to create a spy and writing my spec for the above functon.
describe("The launchTask function", () => {
beforeEach(() => {
global.external.MyObjectFactory= jasmine.any(Function);
spyOn(global.external, 'MyObjectFactory').and.callThrough();
jasmine.createSpyObj("global.external.MyObjectFactory", ["add"]);
});
it("Scene 1", () => {
launchTask(123);
expect(global.external.MyObjectFactory).toHaveBeenCalledWith("INDEXED")
expect(global.external.MyObjectFactory("INDEXED").add).toHaveBeenCalledWith(123)
});
});
The first expect is passing without any errors where as the second expect is giving me an error "plan.apply is not a function"
You haven't actually attached the function add() to MyObjectFactory. Try something like this:
describe("The launchTask function", () => {
let spyObj;
beforeEach(() => {
global.external.MyObjectFactory= jasmine.any(Function);
spyObj = jasmine.createSpyObj(["add"]);
spyOn(global.external, 'MyObjectFactory').and.returnValue(spyObj);
});
it("Scene 1", () => {
launchTask(123);
expect(global.external.MyObjectFactory).toHaveBeenCalledWith("INDEXED");
expect(spyObj.add).toHaveBeenCalledWith(123);
});
});
Related
I'm having a problem with tests. I'm getting error:
TypeError: Cannot read property 'pipe' of undefined
method in directive:
#HostListener('mouseenter')
onMouseenter(): void {
forkJoin([
this.dateService.formatDate(dateStart).pipe(take(1)),
this.dateService.formatDate(dateEnd).pipe(take(1))
]).subscribe((dates) => {
this.showTooltip(`${dates[0]} - ${dates[1]}`);
});
}
(formatDate returns Observable string)
in test:
fit('should call mouse enter', () => {
directive.onMouseenter();
expect(directive.showTooltip).toHaveBeenCalled();
});
should i mock pipe somehow?
thanks!
test looks like this
fdescribe('TooltipDirective', () => {
let directive: TooltipDirective;
let mockDateService: jasmine.SpyObj<DateService>;
beforeEach(() => {
mockDateService = jasmine.createSpyObj('dateService', ['formatDate']);
directive = new TooltipDirective(null, null, mockDateService);
directive.tooltipData = {dateStart: new Date(), endDate: Date()};
spyOn(directive, 'showTooltip');
});
fit('should show tooltip', () => {
directive.onMouseenter();
expect(directive.showTooltip).toHaveBeenCalled();
});
});
const expect = require("chai").expect;
class Test
{
constructor(){ this.x= 10;}
run() {
describe("test goes here", function() {
it("sample test", function() {
expect(this.x).to.be.eq(10);
});
});
}
}
new Test().run();
getting x is undefined.
Issue : this inside describe points to complete different context, how to make x available to this inside mocha test
Use arrow functions () => this... or .bind on your functions.
describe("test goes here", () => {
it("sample test", () => {
expect(this.x).to.be.eq(10);
});
});
MethodToBeTested() {
this.serviceA.methodA1().subscribe((response) => {
if (response.Success) {
this.serviceA.methodA2().subscribe((res) => {
this.serviceB.methodB1();
})
}
});
}
Here is the scenario.
Things to test:
serviceA.methodA1(). was called.
if response.Success then check if serviceA.methodA2() was called
check if serviceB.methodB1() was called when serviceA.methodA2() received value.
first, one is easy to test.
let spy = spyOn(serviceA, 'methodA1');
expect(spy).toHaveBeenCalled();
But does one test 2 and 3?
let spy= spyOn(serviceA, 'methodA1').and.returnValue({subscribe: () => {success:true}});
subject.MethodToBeTested();
something like that?
Alright, so I figured out what I am looking for is callFake
it('should test inside of subscribe', () => {
let spy = spyOn(serviceA, 'methodA1').and.callFake(() => {
return of({ success: true });
});
let spy2 = spyOn(serviceA, 'methodA2').and.callFake(() => {
return of({ success: true });
});
let spy3 = spyOn(serviceB, 'methodB1').and.returnValue(of({ success: true }));
subject.MethodToBeTested();
expect(spy3).toHaveBeenCalled();
});
I learned that returnValue won't actually execute the inside of the subscribe while callFake will with the data you provide inside it.
It would be better to not use a nested subscribe.
Something like this could be a sollution:
let $obs1 = this.serviceA.methodA1().pipe(share());
let $obs2 = $obs1.pipe(switchMap(x => this.serviceA.methodA2()));
$obs1.subsribe(logic1 here...);
$obs2.subsribe(logic2 here...);
I can't figure out why this.sendDrag('started') returns this error:
"Uncaught TypeError: this.sendDrag is not a function"
methods: {
sendDrag (val) {
console.log(val)
}
[...]
mounted () {
this.$nextTick(() => {
this.$refs.flickity.on('dragStart', function () {
this.stageDragging = true
this.sendDrag('started')
})
What causes the error and how to fix it?
You need to capture the value of this in the closure because you are calling it from a function that has a different this.
If you used the arrow-lambda notation ()=>{} instead of function() it would capture this for you automatically. And that is the real difference between the two notations.
mounted () {
this.$nextTick(() => {
const that = this;
this.$refs.flickity.on('dragStart', function () {
that.stageDragging = true
that.sendDrag('started')
})
I have 2 functions where one calls the other and the other returns something, but I cannot get the test to work.
Using expect(x).toHaveBeenCalledWith(someParams); expects a spy to be used, but I am unaware of how to spy on a function within the same file...
Error: : Expected a spy, but got Function.
Usage: expect().toHaveBeenCalledWith(...arguments)
Example.ts
doSomething(word: string) {
if (word === 'hello') {
return this.somethingElse(1);
}
return;
}
somethingElse(num: number) {
var x = { "num": num };
return x;
}
Example.spec.ts
fake = {"num": "1"};
it('should call somethingElse', () => {
component.doSomething('hello');
expect(component.somethingElse).toHaveBeenCalledWith(1);
});
it('should return object', () => {
expect(component.somethingElse(1)).toEqual(fake);
});
In your Example.spec.ts, just add a spyOn(component, 'somethingElse'); as first line of your it('should call somethingElse ... test :
fake = {"num": "1"};
it('should call somethingElse', () => {
// Add the line below.
spyOn(component, 'somethingElse');
component.doSomething('hello');
expect(component.somethingElse).toHaveBeenCalledWith(1);
});
it('should return object', () => {
expect(component.somethingElse(1)).toEqual(fake);
});
The expect method needs a Spy as parameter when used before a toHaveBeenCalledWith (as per the Jasmine documentation).