2016-06-30 2 views
0

웹 개발 노드 & 익스프레스, 5 장 (품질 보증). 모카 (Mocha)와 차이 (Chai)를 설정했지만 테스트에 = 테스트 1을 추가하더라도 테스트가 나타나지 않습니다.모카 테스트가 보이지 않음

meadowlark.js (주 엔트리 포인트)

var express = require('express'); 
var app = express(); 
var handlebars = require('express-handlebars').create({ defaultLayout: 'main' }); 

// express handles normalizing url 

app.set('port', process.env.PORT || 3000); 
app.engine('handlebars', handlebars.engine); 
app.set('view engine', 'handlebars'); 
app.use(express.static(__dirname + '/public')); 

app.use(function(req, res, next) { 
    res.locals.showTests = app.get('env') !== 'production' && req.query.text === '1'; 
    next(); 
}); 

app.get('/', function(req, res) { 
    res.render('home'); 
}); 

app.get('/about', function(req, res) { 
    res.render('about'); 
}); 

// 404 catch-all handler (middleware) 
app.use(function(req, res) { 
    res.status(404); 
    res.render('404'); 
}); 

// 500 error handler (middleware) 
app.use(function(err, req, res, next) { 
    console.error(err.stack); 
    res.status(500); 
    res.render('500'); 
}); 

app.listen(app.get('port'), function() { 
    console.log('Express started on http://localhost:' + app.get('port') + '; press Ctrl-C to terminate.'); 
}); 

main.handlebars (기본 템플릿)

<!DOCTYPE html> 

<html> 

<head> 
    <title>Meadowlark Travel</title> 
    {{#if showTexts}} 
     <link rel="stylesheet" href="/vendor/mocha.css"> 
    {{/if}} 
<script src="http://code.jquery.com/jquery-3.0.0.js" integrity="sha256-jrPLZ+8vDxt2FnE1zvZXCkCcebI/C8Dt5xyaQBjxQIo=" crossorigin="anonymous"></script> 
</head> 
<body> 
    <header><img src="/img/logo.png" alt="Meadowlark Travel logo"></header> 
    {{{body}}} 

    {{#if showTests}} 
     <div id="mocha"></div> 
     <script src="/vendor/mocha.js"></script> 
     <script src="/vendor/chai.js"></script> 
     <script> 
     mocha.ui('tdd'); 
     var assert = chai.assert; 
     </script> 
     <script src="/qa/tests-global.js"></script> 
     {{#if pageTestScript}} 
     <script src="{{pageTestScript}}"></script> 
     {{/if}} 
     <script>mocha.run();</script> 
    {{/if}} 
</body> 
</html> 

공개/QA/테스트 - global.js (모카 테스트 위치) :

suite('Global Tests', function() { 
    test('page has a valid title', function() { 
     assert(document.title && document.title.match(/\S/) && 
     document.title.toUpperCase() !=== 'TODO'); 
    }); 
}); 

폴더 구조 :

meadowlark.js 
node_modules > *node.js/npm* 
package.json 
public > qa  > tests-global.js 
     > vendor > chai.js, 
       > mocha.css, 
       > mocha.js 
views > home.handlebars 
     > layouts > main.handlebars 

내가 뭘 잘못하고 있으며, 어떻게 만들 수있어 모카 검사가 나타나나요? 추가 파일이 필요한 경우 요청하십시오.

답변

1

두 가지 철자 오류와 구문 오류가 있습니다. meadowlark.js에서

, req.qurey.textreq.query.test로 변경 main.handlebars에서

app.use(function(req, res, next) { 
    res.locals.showTests = app.get('env') !== 'production' && req.query.test === '1'; 
    next(); 
}); 

변경, {{#if showTexts}}{{#if showTests}} 행 :

{{#if showTests}} 
    <link rel="stylesheet" href="/vendor/mocha.css"> 
{{/if}} 

그리고 public/qa/tests-global.js에서, 운영자는 !==보다는 !===되어야 같지 않음 :

test('page has a valid title', function() { 
    assert(document.title && document.title.match(/\S/) && 
    document.title.toUpperCase() !== 'TODO'); 
}); 
+0

감사합니다. 큰 도움이되었지만 여전히 작동하지 않습니다. 지금 어떻게해야합니까? – CDuck

+0

@CDuck 잘 모르겠습니다. 나는 내 컴퓨터에서 그것을 테스트했고 그것은 나를 위해 일하고있다. 검색어 문자열 (예 : 'http : // localhost : 3000 /? test = 1')을 입력 했습니까? 그리고 서버 측 터미널이나 클라이언트 측 콘솔에서 오류가 발생 했습니까? – McMath

+0

그런 다음 세 가지 변경 사항을 적용하면 더 많은 정보가 필요할 것입니다. 디렉토리 구조는 어떻게 생겼습니까? – McMath

관련 문제