Make module load JSON Karma AngularJS











up vote
0
down vote

favorite












I have a project built in AngularJS and I want to add unit test using Jasmine and Karma. Everytime I try to run a test I get 'Failed to instantiate module' because in the source code I load a local json file using XMLHttpRequest but in the test the same function respond with a 404. The json is a configuration file so if it's not loaded all the app can not be loaded properly. I tried with $httpBackend to capture the GET request and answer with a custom json but seems that $httpBackend cannot capture the request.



EDIT



I think the problem is the same of this










share|improve this question




























    up vote
    0
    down vote

    favorite












    I have a project built in AngularJS and I want to add unit test using Jasmine and Karma. Everytime I try to run a test I get 'Failed to instantiate module' because in the source code I load a local json file using XMLHttpRequest but in the test the same function respond with a 404. The json is a configuration file so if it's not loaded all the app can not be loaded properly. I tried with $httpBackend to capture the GET request and answer with a custom json but seems that $httpBackend cannot capture the request.



    EDIT



    I think the problem is the same of this










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a project built in AngularJS and I want to add unit test using Jasmine and Karma. Everytime I try to run a test I get 'Failed to instantiate module' because in the source code I load a local json file using XMLHttpRequest but in the test the same function respond with a 404. The json is a configuration file so if it's not loaded all the app can not be loaded properly. I tried with $httpBackend to capture the GET request and answer with a custom json but seems that $httpBackend cannot capture the request.



      EDIT



      I think the problem is the same of this










      share|improve this question















      I have a project built in AngularJS and I want to add unit test using Jasmine and Karma. Everytime I try to run a test I get 'Failed to instantiate module' because in the source code I load a local json file using XMLHttpRequest but in the test the same function respond with a 404. The json is a configuration file so if it's not loaded all the app can not be loaded properly. I tried with $httpBackend to capture the GET request and answer with a custom json but seems that $httpBackend cannot capture the request.



      EDIT



      I think the problem is the same of this







      angularjs json karma-jasmine karma-runner






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 at 13:21

























      asked Nov 20 at 15:35









      dvdpzzt

      638




      638
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          You can use mock data from JSON file by picking it up using require() function:



          const data: any = require('../../assets/sample-data.json');


          Now you should be able to use data object in your tests.
          Or You can convert your json data to an Angular service (actually an Angular constant can work). Then my external file will be easy to grab in the tests, ex:



          var setupData = $injector.get('SampleData');
          $httpBackend.whenGET('/my-service-url').respond(SampleData);
          $httpBackend.flush();





          share|improve this answer





















          • Tank you for the response. I tried some stuff including yours but the problems seems to be when the module is injected, that happen before the execution of your code. It seems like jasmine or karma cannot include the file when creating the test environment. I tried to add the path in the karma.conf.js with no success
            – dvdpzzt
            Nov 21 at 11:19













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396425%2fmake-module-load-json-karma-angularjs%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          You can use mock data from JSON file by picking it up using require() function:



          const data: any = require('../../assets/sample-data.json');


          Now you should be able to use data object in your tests.
          Or You can convert your json data to an Angular service (actually an Angular constant can work). Then my external file will be easy to grab in the tests, ex:



          var setupData = $injector.get('SampleData');
          $httpBackend.whenGET('/my-service-url').respond(SampleData);
          $httpBackend.flush();





          share|improve this answer





















          • Tank you for the response. I tried some stuff including yours but the problems seems to be when the module is injected, that happen before the execution of your code. It seems like jasmine or karma cannot include the file when creating the test environment. I tried to add the path in the karma.conf.js with no success
            – dvdpzzt
            Nov 21 at 11:19

















          up vote
          0
          down vote













          You can use mock data from JSON file by picking it up using require() function:



          const data: any = require('../../assets/sample-data.json');


          Now you should be able to use data object in your tests.
          Or You can convert your json data to an Angular service (actually an Angular constant can work). Then my external file will be easy to grab in the tests, ex:



          var setupData = $injector.get('SampleData');
          $httpBackend.whenGET('/my-service-url').respond(SampleData);
          $httpBackend.flush();





          share|improve this answer





















          • Tank you for the response. I tried some stuff including yours but the problems seems to be when the module is injected, that happen before the execution of your code. It seems like jasmine or karma cannot include the file when creating the test environment. I tried to add the path in the karma.conf.js with no success
            – dvdpzzt
            Nov 21 at 11:19















          up vote
          0
          down vote










          up vote
          0
          down vote









          You can use mock data from JSON file by picking it up using require() function:



          const data: any = require('../../assets/sample-data.json');


          Now you should be able to use data object in your tests.
          Or You can convert your json data to an Angular service (actually an Angular constant can work). Then my external file will be easy to grab in the tests, ex:



          var setupData = $injector.get('SampleData');
          $httpBackend.whenGET('/my-service-url').respond(SampleData);
          $httpBackend.flush();





          share|improve this answer












          You can use mock data from JSON file by picking it up using require() function:



          const data: any = require('../../assets/sample-data.json');


          Now you should be able to use data object in your tests.
          Or You can convert your json data to an Angular service (actually an Angular constant can work). Then my external file will be easy to grab in the tests, ex:



          var setupData = $injector.get('SampleData');
          $httpBackend.whenGET('/my-service-url').respond(SampleData);
          $httpBackend.flush();






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 18:51









          nkuma_12

          69714




          69714












          • Tank you for the response. I tried some stuff including yours but the problems seems to be when the module is injected, that happen before the execution of your code. It seems like jasmine or karma cannot include the file when creating the test environment. I tried to add the path in the karma.conf.js with no success
            – dvdpzzt
            Nov 21 at 11:19




















          • Tank you for the response. I tried some stuff including yours but the problems seems to be when the module is injected, that happen before the execution of your code. It seems like jasmine or karma cannot include the file when creating the test environment. I tried to add the path in the karma.conf.js with no success
            – dvdpzzt
            Nov 21 at 11:19


















          Tank you for the response. I tried some stuff including yours but the problems seems to be when the module is injected, that happen before the execution of your code. It seems like jasmine or karma cannot include the file when creating the test environment. I tried to add the path in the karma.conf.js with no success
          – dvdpzzt
          Nov 21 at 11:19






          Tank you for the response. I tried some stuff including yours but the problems seems to be when the module is injected, that happen before the execution of your code. It seems like jasmine or karma cannot include the file when creating the test environment. I tried to add the path in the karma.conf.js with no success
          – dvdpzzt
          Nov 21 at 11:19




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396425%2fmake-module-load-json-karma-angularjs%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Feedback on college project

          Futebolista

          Albești (Vaslui)