Setup Jasmine Test Framework on MVC 5

2019-07-21 23:14发布

问题:

I am having problems setting up the Jasmine Test Framework from nuget in my MVC 5 project in VS2013.

I always get an error message when I run the SpecRunner.cshtml file. It keeps saying that the resource cannot be found but it doesn't specify which one.

Anyone know how to properly setup a standalone SpecRunner.cshtml (without needing to go through the index page)?

Here is what I currently have in my SpecRunner.cshtml

@{
  Layout = null;
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Toolkit - Tests</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link rel="shortcut icon" type="image/png" href="/Content/jasmine/jasmine_favicon.png">
    <link rel="stylesheet" type="text/css" href="/Content/jasmine/jasmine.css">
    <script type="text/javascript" src="/Scripts/jasmine/jasmine.js"></script>
    <script type="text/javascript" src="/Scripts/jasmine/jasmine-html.js"></script>
    <script type="text/javascript" src="/Scripts/jasmine/boot.js"></script>

    <script src="~/Scripts/AngularJS/angular.js"></script>
    <script src="~/Scripts/AngularJS/angular-mocks.js"></script>
    <script src="~/Scripts/AngularJS/angular-ui-router.js"></script>

    <!-- include source files here... -->
    <!--<script type="text/javascript" src="/Scripts/jasmine-samples/SpecHelper.js"></script>
    <script type="text/javascript" src="/Scripts/jasmine-samples/PlayerSpec.js"></script>-->
    <script src="~/Scripts/MyAngularJS/app.js"></script>
    <script src="~/Scripts/MyAngularJS/controllers.js"></script>

    <!-- include spec files here... -->
    <!--<script type="text/javascript" src="/Scripts/jasmine-samples/Player.js"></script>
    <script type="text/javascript" src="/Scripts/jasmine-samples/Song.js"></script>-->
    <script src="~/Scripts/MyAngularJS/controllersTest.js"></script>

    <script>
    (function () {
        var jasmineEnv = jasmine.getEnv();
        jasmineEnv.updateInterval = 1000;

        var htmlReporter = new jasmine.HtmlReporter();

        jasmineEnv.addReporter(htmlReporter);

        jasmineEnv.specFilter = function (spec) {
            return htmlReporter.specFilter(spec);
        };

        var currentWindowOnload = window.onload;

        window.onload = function () {
            if (currentWindowOnload) {
                currentWindowOnload();
            }
            execJasmine();
        };

        function execJasmine() {
            jasmineEnv.execute();
        }

    })();
    </script>
</head>

<body>
</body>
</html>

回答1:

You could use Chutzpah. It is a JavaScript test runner which works fine with Jasmine. It is available as a plug-in for Visual Studio and is easy to setup. It also integrates with the Test Explorer of Visual Studio. The wiki explains the features and how it works.