jejacks0n / teaspoon

Teaspoon: Javascript test runner for Rails. Use Selenium, BrowserStack, or PhantomJS.
1.43k stars 243 forks source link

loading javascript fixture data doesn't appear to work #330

Closed rstudner closed 9 years ago

rstudner commented 9 years ago

if I have this file (fakeGraphData.js):

var FAKE_ED_GRAPH_DATA = {
  'dates': ['2014-04', '2014-05', '2014-06', '2014-07', '2014-08',
    '2014-08', '2014-10', '2014-11', '2014-12', '2015-01', '2015-02', '2015-03'],
  'high': {
    'org': [1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2],
    'all_average': [3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 2]
  },
  'medium': {
    'org': [3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 2],
    'all_average': [1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2]
  },
  'low': {
    'org': [1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2],
    'all_average': [3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 2]
  }
};

and I do this.fixtures = fixture.load('index.html', 'fakeGraphData.js');

The variable FAKE_ED_GRAPH_DATA is not 'on the page'

In the chrome console I see: undefined: {undefined: [3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 2]}

jejacks0n commented 9 years ago

you can't load javascript with the fixtures.. it doesn't eval it, and so your var won't get processed. either load your js file in your test run like you would load any other javascript, or use a proper json format.

rstudner commented 9 years ago

So in the documentation, when it demonstrates loading "fixture.js" what exactly was happening there?

jejacks0n commented 9 years ago

you mean here: https://github.com/modeset/teaspoon#example-usage -- where it clearly uses .json and not .js?

rstudner commented 9 years ago

Sure - what would be in that file for example, that a test then could reasonably expect to be able to use?

Just trying to use this awesome framework and work and clarify how parts of it work -- not sure why you keep responding so aggressively.

jejacks0n commented 9 years ago

sorry if it comes across that way, just trying to give you the most concise information. if you have javascript, load it as a dependency of your test. if you have json data, load it via fixtures.

jejacks0n commented 9 years ago

either works in your case depending on what you want, but when I say the fixtures don't eval javascript, it should be clear what the intent is. it's to load raw data, not javascript that is parsed.

rstudner commented 9 years ago

Totally understand -- and this is opensource which means people (like me) should be happy with the docs we get (if they exist at all haha).

I guess i'm just at a loss for how "loading JSON as fixture data" could actually/possibly help with a test if I can't set it to a javascript variable (i.e. javascript code) while i'm at it haha. I don't get how 'load raw data' helps with writing a test case, if I can't reference the raw data :)

Clearly i'm missing the obvious -- sorry.

No biggie -- I moved it into my tests, just always looking to explore more about what I can/can't pull off from a reuse POV etc.

jejacks0n commented 9 years ago

ok, if the documentation isn't clear, I don't know how to help you.

data.json

{
  'dates': ['2014-04', '2014-05', '2014-06', '2014-07', '2014-08',
    '2014-08', '2014-10', '2014-11', '2014-12', '2015-01', '2015-02', '2015-03'],
  'high': {
    'org': [1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2],
    'all_average': [3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 2]
  },
  'medium': {
    'org': [3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 2],
    'all_average': [1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2]
  },
  'low': {
    'org': [1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2],
    'all_average': [3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 2]
  }
}
fixture.load('data.json')
// within your tests you will have fixture.json[0] -- which is that variable.

as you can see, your js is not valid json -- double quotes are required for string variables. http://json.org/

rstudner commented 9 years ago

I found in the pre block, of the preload function example, fixture.json[0]. I had missed that prior -- my bad. (and searching the page for fixture.json[ doesn't find it either due to the pre block).

Sweet! thanks :)