问题
I have unit tests for an application where the javascript I wanted to test was dependent on other JS frameworks (underscore.js, backbone.js, jQuery...). So when setting up the specRunner.html I added these files as well:
....
<head>
<title>p13n.js Spec Runner</title>
<link rel="shortcut icon" type="image/png" href="./jasmine-standalone-1.3.1/lib/jasmine-1.3.1/jasmine_favicon.png">
<link rel="stylesheet" type="text/css" href="./jasmine-standalone-1.3.1/lib/jasmine-1.3.1/jasmine.css">
<script src="./jasmine-standalone-1.3.1/lib/jasmine-1.3.1/jasmine.js"></script>
<script src="./jasmine-standalone-1.3.1/lib/jasmine-1.3.1/jasmine-html.js"></script>
<!-- include source files here... -->
<script src=".../jquery.min.js" ></script>
<script src=".../underscore-min.js"></script>
<script src=".../backbone-min.js"></script>
<script src=".../MyApplication-file.js"></script>
<!-- include spec files here... -->
<script src=".../spec/MyApplication-spec.js"></script>
<script type="text/javascript">
(function() {
var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 1000;
var htmlReporter = new jasmine.HtmlReporter();
...
However, this does not work for automated testing using a tool like Grunt. I can't rely on the the specRunner.html to load my dependencies, as the .html is not used. How do you manage dependencies?
- should I be testing a minified and compressed version of my app code + vendor?
- should I be using a tool like require.js within my unit test spec?
- does Grunt solve for this another way?
回答1:
Depending on your IDE or Editor you are using you can specify dependencies in the same way you add references to files to get code completion.
E.g. in Visual Studio for example the format used is:
/// <reference path="~/lib/jasmine-2.0.0/jasmine.js" />
/// <reference path="~/lib/jasmine-2.0.0/jasmine-html.js" />
/// <reference path="~/lib/jasmine-2.0.0/console.js" />
/// <reference path="~/lib/jasmine-2.0.0/boot.js" />
/// <reference path="../../../../<ProjectName>/javascript/shared/jquery-1.7.2.min.js" />
/// <reference path="../../../../<PorjectName>/bundles/app.javascript.js" />
Where <ProjectName> is your project name and this is only the case as the Test Project is a separate Project from the main App
来源:https://stackoverflow.com/questions/22466651/jasmine-specs-without-specrunner-html-js-dependencies