Chutzpah running Jasmine in TFS 2012 can't fin

2019-05-07 22:44发布

问题:

I am using Chutzpah to run our Jasmine tests. I have added the Chutzpah dlls to the solution and updated the build to run *.js tests.

The project structure is as follows:

MyApp.Web
    Scripts
        App
            Home 
                DateControl.js 
MyApp.Web.Tests 
    Scripts 
        Jasmine 
        lib
            Chutzpah (dlls) 
        Spec 
            App 
                Home 
                    DateControlSpecs.js

The Jasmine test file uses a reference tag to reference the file to be tested

/// <reference path="../../../../../../App.web/scripts/app/home/datecontrol.js" />

The Jasmine tests are run however I get the following error:

ReferenceError: Can't find variable: dateControl in file

dateControl is the object under test. If I copy the code to be tested into the Jasmine test file then the tests pass. The jasmine tests with the reference tag pass using the Chutzpah Test Adapter in Test Explorer in Visual Studio 2013.

I have no idea why this is not working.

Update

@jessehouwing's answer pointed out the route of the issue for me.

The build folder on the build server has the following structure

bin _PublishedWebsites src

The jasmine test scripts and libraries are copied to bin whereas the script files under test are copied to both the src directory and the _publishedwebsite directory.

I am looking for an easy to manage solution so that any future devs do not need to know they need to link any new scripts into the test project.

回答1:

Solution I can live with for now

For both the jasmine test files and the files under test I added "Always copy" in properties as per the Chutzpah documentation. I didn't realise that the files under test were actually being copied to the builds bin folder along with the jasmine test files. So on the build server I ended up with the following struacture

bin
    scripts
        app
            * application js files
        specs
            * jasmine test files

In my jasmine test file I add two references:

  • The first to the Visual Studio directory sturcture of the file to be tested
  • The second to the build bin folder structure

Hack 1

I can get it to work by adding another reference to my jasmine test file which uses the structure of the build directory and not the solution directory structure but again this is open to error as the path may be typed in incorrectly.

The second reference below will allow the tests to run in both VS Test Explorer and TFS Build

/// <reference path="../../../../../../App.Web/scripts/app/home/datecontrol.js" />
/// <reference path="../../../../../_PublishedWebsites\App.Web/scripts/app/home/datecontrol.js" />

Hack 2

By adding an xcopy command to the post-build build event of the project I can copy the files to be tested into the same location as the jasmine test files.



回答2:

This is most likely caused by the fact that Team Build separates the Sources and Binaries folders and enforces a different output structure in the Binaries folder as well. This means your files are not in the place you expect them to be.

To ensure the test runner can always find your references you must use the "Add as Link" option in Visual Studio and set their build action to "Copy Always" so that they're copied to the test directory upon test execution.

Plus, you might need to enable "Deployment" in the Test configuration in your Build Definition, as outlined here.