How to use spy from jasmine

How to use spy from jasmine



I am very new to Angular and jasmine and I am facing issues while I am doing mock:


public uploadFile(confirm)
this.uploadModalRef.close();
if (this.filePath.length)
let ngbModalOptions: NgbModalOptions =
backdrop : 'static',
keyboard : 'false',
windowClass: 'custom-class'
;
this.importModalRef = this.modalservice.open(confirm, ngbModalOption);




Here is what I am trying:


it('should upload the file', () =>
let close: "11";
let filepath;
component.uploadFile;
expect(component.uploadFile([filePath]) = ['11'].toBe(close);
);



But still if conditions was highlighted in code coverage and this.uploadModalref


this.uploadModalref



Please let me know what iam doing wrong here.





It seems like your test is not completed yet and has some bugs. I've tried somehow to edit it but didn't understand the aim of your test. Could you fix it - what you're expecting from your test file?
– shohrukh
Aug 28 at 13:08





Hi thanks for the reply actually iam trying cover my code using above spec but while iam running the code coverage if conditions have not covered and else part not covered actually I don’t how to do
– Mahadevan
Aug 28 at 13:12





Got it. Added an example in the answers, please check it out.
– shohrukh
Aug 28 at 14:48




1 Answer
1



I've created simple unit test for uploadFile method: the test expects that modalService.open will be called with mocked params when we have non-empty filePath array:


uploadFile


modalService.open


filePath


describe('HelloComponent', () =>
let fixture: ComponentFixture<TestComponent>;
let component: HelloComponent;
const modalService: NgbModal = jasmine.createSpyObj('modalService', ['open']);

beforeEach(() =>
TestBed.configureTestingModule(
declarations: [ HelloComponent, TestComponent ],
providers: [
provide: NgbModal,
useValue: modalService
]
);

fixture = TestBed.createComponent(TestComponent);
component = fixture.debugElement.children[0].componentInstance;

fixture.detectChanges();
);

it('should call modalService.open on uploadFile', () =>
component.filePath = 'File1';
component.uploadModalRef = jasmine.createSpyObj('uploadModalRef', ['close']);
let mockOptions: NgbModalOptions =
backdrop : 'static',
keyboard : false,
windowClass: 'custom-class'
;
const mockConfirm = 'confirm-template';

component.uploadFile(mockConfirm);

expect(modalService.open).toHaveBeenCalledWith(mockConfirm, mockOptions);
);
);



Since your component depends on NgbModal we have to mock this injector and for this we can create jasmine spy object:


NgbModal


const modalService: NgbModal = jasmine.createSpyObj('modalService', ['open']);



Then we provide NgbModal into test module providers using our created spy object. This will allow us to spy on it methods (for this example it's open method).


NgbModal


open



In the test itself we follow AAA pattern: arrange act assert. First we arrange class properties and method arguments by creating mock data. Then we call the target function uploadFile. And lastly - we're expecting that modalService.open method will be called with correct arguments. You can also add another unit tests based on this example by changing mock data. For example:


uploadFile


modalService.open


it('should not call modalService.open on uploadFile when filePath is empty', () =>
component.filePath = '';
component.uploadModalRef = jasmine.createSpyObj('uploadModalRef', ['close']);
const mockConfirm = 'confirm-template';

component.uploadFile(mockConfirm);

expect(modalService.open).not.toHaveBeenCalled();
);



Since there is an if statement in the uploadFile method, the modalService.open method will not be called if the filePath array is empty. This is exactly what we expect in the last example.


uploadFile


modalService.open


filePath



Also created a stackblitz demo, so you can check it out here.





Thank you so much for your help
– Mahadevan
Aug 28 at 17:06





you are welcome :)
– shohrukh
Aug 28 at 17:22





Hi @sherlock.92 I am getting error while implementing in my code ‘type string is not assignable to type string’ for component.filePath = [‘file’]; for this line kindly let me know what iam doing wrong here
– Mahadevan
Aug 29 at 4:56





Sorry, my mistake. I thought your file path is an array. So I've updated the answer. In your tests you have to write component.filePath = 'File1'; or component.filePath = ''; if you want to test another case.
– shohrukh
Aug 29 at 5:24


component.filePath = 'File1';


component.filePath = '';





Hi one more request if say else path not covered please let me know what I have to do
– Mahadevan
Aug 29 at 5:29






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ