2

Simply put, in the end, i want to grab the query string from the url and add it to my cookie.

I know how to get the cookie set and am using Vanilla JS for it.

However I want to use Jasmine to test all the wrong cases and not just the happy path.

Anyways, How does one set the url in Jasmine, so that I can pass any query string (like http://www.blah.com?ref=yayayadaydayday so that i can use it in my tests using document.location.search and take it from there?

I'm guessing whatever it is called when you mock in Jasmine, but if this was rails, i'd add a query string like:


  test "can get some results" do
    get :show, :q => shops(:one).name #how do i add this 'q' string in jasmine?!
    refute_nil assigns(:search)
  end
pjammer
  • 9,489
  • 5
  • 46
  • 56

2 Answers2

3

The problem is that you cant mock window.location. In this and this SO they came up with passing the global context in you function so you can mock it.

Community
  • 1
  • 1
Andreas Köberle
  • 106,652
  • 57
  • 273
  • 297
  • 1
    Thanks, those two links look good with many solutions to try. I read on the jasmine board, you can't do it anyways, because it's just javascript. Makes me think, isn't there a 'real' test framework that I should use, but that is just the cynic in me i guess. thanks. – pjammer Aug 02 '12 at 22:49
2

I recently had to do something similar, testing some code for a set of tabs which selects a tab if the URL hash is set. I used the history API to manipulate the URL:

loadFixtures('tabs-fixtures.html');
window.history.replaceState(null, null, '#three');

and the code does indeed read the hash value from the URL.

my test setup uses PhantomJS. I imagine other browsers would work, unless you have an old IE launcher.

ben.tiberius.avery
  • 2,194
  • 1
  • 13
  • 8