Strategies for removing duplication

66 views
Skip to first unread message

tom.he...@logicgate.com

unread,
Jan 30, 2019, 3:05:00 PM1/30/19
to Jasmine
I have an immediate problem, and a more general problem.

The test suite I'm currently working on verifies that our UI does the right thing when users upload a file. Large files are handled somewhat differently than average-size files, so we'd like to run the same tests for two different tests files.

I wrote 3 spec files for the suite, with the small file. The cut-and-paste solution would be, copy those specs and just change the path to the test file. But what's a better solution?

More generally: I use page objects to remove a lot of repetition when it comes to the various actions I need to perform with our UI. But I don't know the general approach for removing repetition in the tests themselves. I'm the only person at my shop working with Protractor, so I'm not getting a lot of intensive code review. I'm very interested in whatever blog posts or libraries folks can recommend so that I can educate myself.

Gregg Van Hove

unread,
Feb 5, 2019, 1:48:55 PM2/5/19
to jasmi...@googlegroups.com
The most straight-forward way to do this is probably to write a function that defines the subset of shared behaviors that you have and call that function from within the different `describes`. You can either use the Jasmine-provided `this` and a `beforeEach`/`beforeAll` or pass information into the function if you know it at suite declaration time. This would look something like:

function shared() {
  it('uploads', function() {
    upload(this.file);
  });
}

describe('big', function() {
  beforeEach(function() {
    this.file = getBigFile();
  });
  shared();
});

or

function shared(fileName) {
  it('uploads', () => { upload(fileName) });
}

describe('big, () => {
  shared('bigFile.txt');
});

Hope this helps. Thanks for using Jasmine!

- Gregg

--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+...@googlegroups.com.
To post to this group, send email to jasmi...@googlegroups.com.
Visit this group at https://groups.google.com/group/jasmine-js.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages