Why is Karma refusing to serve my JSON fixture (which I'd like to use in my jasmine / angularjs tests)

后端 未结 3 1698
清酒与你
清酒与你 2021-01-06 09:40

As indicated in this stackoverflow answer, it looks like Karma will serve JSON fixtures. However, I\'ve spent too many hours trying to get it to work in my environment. Re

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-06 09:54

    So, I had a lot of issues with jasmine-jquery and I got a pretty decent workaround.

    It's a little hacky, but it works. Basically, I just create a function accessible on the window, then stack the JSON fixtures inside a little switch:

    if (typeof(window.fixtures === "undefined")) {
      window.fixtures = {};
    }
    
    window.setFixture = function(type) {
      var json;
    
      if (type == "catalog") {
        json = { ... }
      }
    
    if (typeof(type) !== "undefined") {
      window.fixtures[type] = json;
    }
      return json;
    
    }
    

    Then, I can just stub it inline in the view:

    describe "App.Models.Catalog", ->
      it "provides the 'App.Models.Catalog' function", ->
        expect(App.Models.Catalog).toEqual(jasmine.any(Function))
    
      it "sets up a fixture", ->
        setFixture("catalog")
        console.log(fixtures["catalog"])
        expect(fixtures["catalog"]).toBeDefined()
    

    Boom, tests pass, and the object comes out in the log:

    {
      catalog_id: '2212',
      merchant_id: '114',
      legacy_catalog_id: '2340',
      name: 'Sample Catalog',
      status: '1',
      description: 'Catalog Description ',
    }
    

    Now, it's accessible within my test.

    It's of course not perfect or ideal, but I kept hitting strange matchErrors and the like with the jasmine-jquery plugin, and it's simple enough (and fast) for me to paste in a couple of JSON blocks and get moving.

    You also save yourself the time fiddling around with the configuration and making any changes to the files for Karma.

    Anyone have any better suggestions or have any luck getting jasmine-jquery to work?

提交回复
热议问题