52
expect(view.$el.html()).toContain('Admin');

The view does contain the word 'Admin' so I was expecting it to return true. How can I achieve this?

expect(view.$el.html()).toContain('Admin');

This returns undefined. How can I make it return true?

<header class="main">
  <div id="heading">
    <ul class="header-view right">
      <li class="inline"><a href="#link" class="button help-btn tab-btn"><span>  </span>Help</a></li>
      <li class="inline last"><a href="#og" class="button admin-btn tab-btn expand-admin"><span></span>Admin</a></li>
    </ul>
  </div>
</header>

This is what is returned from view.$el.html

Please help.

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
Spdexter
  • 923
  • 1
  • 7
  • 19
  • 2
    please share some relevant html code and mention all jquery libraries that you are using. Also try to reproduce your problem on jsfiddle link and share it. – Bhushan Kawadkar Mar 31 '15 at 11:56
  • `view.$el.html()` what it returns – Sudharsan S Mar 31 '15 at 12:22
  • @BhushanKawadkar i am new to jasmine- i dont know if i can use jsfiddle for it! i only use jquery-backbone. No other libraries – Spdexter Mar 31 '15 at 12:36
  • Hi Spdexter, would you mind to update the correct answer as the one provided by @alecxe please? The actual one marked as correct is outdated. – quirimmo Aug 12 '17 at 13:43

2 Answers2

92

toContain() now can actually be used for substring in string checks:

expect(view.$el.html()).toContain('Admin');
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Somehow this is not working for me error ===> ✗ change version: BData details page - Expected 'https://xxx/data-objects/NS_PROTRACTOR_TEST_DL42/DATA_LINEAGE_TEST/PRC/ORC/0/versionTest/0' to match '/data-objects/NS_PROTRACTOR_TEST_DL42/DATA_LINEAGE_TEST/PRC/ORC/0/versionTest/0;'. I am using contain to check substring – Aniruddha Das Mar 23 '18 at 14:35
  • Actually, it does work - I was just doing the check against the nativeElement instead of the actual text. My bad! – Wagner Danda da Silva Filho Jan 24 '19 at 17:12
  • 4
    `const content: HTMLElement = fixture.debugElement.nativeElement; expect(content.innerHTML).toContain('Admin');` for those looking for equivalent on angular 2. – Ambroise Rabier Feb 18 '19 at 10:59
18

From the Jasmine docs:

The 'toContain' matcher is for finding an item in an Array

You are trying to find a string inside a string so I would do something like this:

expect(view.$el.html().indexOf('Admin') !== -1).toBe(true);
Lee Bailey
  • 3,594
  • 2
  • 18
  • 17
  • 6
    `toMatch` can also be used: `expect(view.$el.html()).toMatch('Admin')`. The `toMatch` matcher is for regular expressions. – Tomás Fox Sep 02 '15 at 16:34
  • 4
    Please update the answer as this is both a bad way of solving it and outdated. – hakunin Aug 25 '16 at 09:37