0

I'm new to client-side testing, but have written a Jasmine test in Visual Studio that runs my JS code against an HTML "fragment" (part of my page) and successfully performs an Assert - all well and good.

However, I'm not able to load the HTML from a test fixture as described in the documentation. I've seen several similar posts on this subject, but have followed their examples, but the code syntax isn't recognized. I've temporarily got around this by placing my HTML in a separate function and reading this when needed. Whilst this works, it's unsatisfactory since I need to escape the multi-line HTML with a back-slash character at the end of every line, and I don't fancy doing this for all the hundreds of tests that I envisage I'll end up writing.

Okay, my setup.

  • Visual Studio 2013
  • Sinon 1.17.2
  • Chutzpah Test Adapter For the Test Explorer 4.1.0
  • Chutzpah Test Runner Context Menu Extension 4.1.0

I have the following directory structure in my project, things in square brackets [xyz] are folders, and the "\" signifies child in directory structure

Project
\ [MyApplicationName]
\\ [FeatureUnderTest]
\\\ myTestFile.js
\\\ myHtmlFragmentFromPage.html
\\\ myHtmlFragmentReturnedFromAjax.html
\ [Libraries]
\\ sinon-1.17.2.js

Okay, so as you can see, I have my test file in the SAME folder as my test file. I would probably end up with multiple tests for each Feature - there may be sub-features with their associated tests too. And ideally, I'd like the HTML files to be in a sub folder relative to the Test JS, and this folder could have a standard name such as "Fixtures". So I could end up with something like:

Project
\ [MyApplication]
\\ [Feature1]
\\\ [Fixture]
\\\\ HtmlFragA.html
\\\\ HtmlFragB.html
\\\ Feature1.Test.js

\\ [Feature2]
\\\ [SubFeature1]
\\\\ [Fixture]
\\\\\ HtmlFragA.html
\\\\\ HtmlFragB.html
\\\\ Feature2.SubFeature1.Test.js
\\\ [SubFeature2]
\\\\ [Fixture]
\\\\\ HtmlFragA.html
\\\\\ HtmlFragB.html
\\\\ Feature2.SubFeature2.Test.js
\\\ [SubFeature3]
\\\\ [Fixture]
\\\\\ HtmlFragA.html
\\\\\ HtmlFragB.html
\\\\ Feature2.SubFeature3.Test.js

\\ [Feature3]
\\\ [Fixture]
\\\\ HtmlFragA.html
\\\\ HtmlFragB.html
\\\ Feature3.Test.js

\ [Libraries]
\\ sinon-1.17.2.js

For those with experience with many tests, is this a good way to segregate these tests? If not, any pointers would be appreciated.

Okay, onto my code.

/// <reference path="../../../myApplication/scripts/Feature1/jsFileUnderTest.js" />
describe("Given Feature 1", function (){
    describe("When AJAX call is successful", function() {
        // Currently load from functions
        var html = htmlFragmentFromDom();
        var ajaxResponse = ajaxResponse();

        beforeEach(function() {
            $('body').append(html);

            sinon.stub($, 'ajax', function (options){
                var dfd = $.Deferred();
                if (options.success) {dfd.done(options.success(ajaxResponse));}
                if (options.error) {dfd.fail(options.error);}
                dfd.success = dfd.done;
                dfd.error = dfd.fail;
                return dfd;
            });
        });    
        afterEach(function() {
            $(html).remove();
            $.ajax.restore();
        });

        if("Then abc Should Be 123", function (){
            ....
        });
    });
});
function htmlFragmentFromDom(){
    return '... /
            .....';
}
function ajaxResponse(){
    return '... /
            .....';
}

The contents of my Chutzpah.json is as follows:

{
    "Framework": "jasmine",
    "TestHarnessLocationMode": "TestFileAdjacent",
    "TestFileTimeout": "120000",
    "References": [
        { "Path": "../../myApplication/scripts/jquery/jquery-1.11.2.js" },
        { "Path": "../../myApplication/scripts/jquery/jquery-ui-1.11.4.js" },
        { "Path": "../libraries/sinon-1.17.2.js" }
    ]
}

So, if anyone knows what I need to do to be able to read in the HTML (for the DOM fragment that the JavaScript-under-test will run on and for the HTML returned from the AJAX call, then I will be most grateful.

Griff

DrGriff
  • 4,394
  • 9
  • 43
  • 92
  • I partially solved this. Thanks to the article http://blog.carbonfive.com/2011/07/06/pragmatic-javascript-testing-with-jasmine/, I added the file jasmine.jQuery-2.1.1.js to my Libraries folder, referenced that in Chutzpah.json and added the lines: jasmine.getFixtures().fixturesPath = './Fixtures/'; and loadFixtures('fragmentFromDom.html'); to my beforeEach function, whilst removing the line: $('body').append(html);. Success!, though I still have to load the HTML returned by the AJAX from a function that is escaped as described above with backslashes. Still need to solve that...ideas? – DrGriff Jan 19 '16 at 08:06

1 Answers1

0

Chutzpah supports injecting HTML templates into the dom. Just reference your HTML file from chutzpah.json and it should work:

"References": [
        { "Path": "myHtmlFile.html" }
    ]
Matthew Manela
  • 16,572
  • 3
  • 64
  • 66
  • My chutzpah.json file is common to all tests (I believe) and each test will only want specific HTML templates. Also, not sure how these would be removed from the DOM (after each test). – DrGriff Jan 28 '16 at 10:46
  • In that case you have two options. 1. Use a /// – Matthew Manela Feb 04 '16 at 01:13