Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何比较html和javascript中的仅输出部分?_Javascript_Jquery_Html - Fatal编程技术网

如何比较html和javascript中的仅输出部分?

如何比较html和javascript中的仅输出部分?,javascript,jquery,html,Javascript,Jquery,Html,我计划为JavaScript测试创建一个应用程序。在这里,我将给出一个关于创建程序的问题,例如,在警报框中显示文本“HTML代码播放”?用户将进行编码,他们可以使用函数、变量等的任何名称 我想检查输出,其中输出显示警报框“HTML代码播放” 用户代码 <script> function showalert() { alert("HTML Code Play"); } </script> <input type="button" value="Show Alert

我计划为JavaScript测试创建一个应用程序。在这里,我将给出一个关于创建程序的问题,例如,在警报框中显示文本“HTML代码播放”?用户将进行编码,他们可以使用函数、变量等的任何名称

我想检查输出,其中输出显示警报框“HTML代码播放”

用户代码

<script>
function showalert()
{
 alert("HTML Code Play");
}
</script>

<input type="button" value="Show Alert" onclick="showalert();"> 
<button onclick="alertbox();">Show Alert</button>

<script>
function alertbox()
{
 alert("HTML Code Play");
}
</script>

函数showalert()
{
警报(“HTML代码播放”);
}
与以下代码(我的代码)进行比较。

显示警报
函数alertbox()
{
警报(“HTML代码播放”);
}
在上面的示例中,两者产生相同的输出,但编码不同。
当我检查两个代码时,我希望结果为true。我能做吗?可能吗?

我发现您的问题很有趣,并使用mocha和sinon创建了示例代码。
(要执行此操作,您必须自己安装sinon.js模块。)
我将此代码作为工作代码发布

请将“代码1”、“代码2”块视为用户实现的代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>How to test user code on broweer</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.5.3/mocha.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.5.3/mocha.css" rel="stylesheet">
    <script type="text/javascript" src="../components/sinon/lib/sinon.js"></script>
    <script type="text/javascript" src="../components/sinon/lib/sinon/util/core.js"></script>
    <script type="text/javascript" src="../components/sinon/lib/sinon/extend.js"></script>
    <script type="text/javascript" src="../components/sinon/lib/sinon/spy.js"></script>
    <script type="text/javascript" src="../components/sinon/lib/sinon/call.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
    <h1>How to test user code on broweer</h1>
    <div id="mocha"></div>

    <div id="live-code-stage"></div>

    <template id="code1">
        <button onclick="alertbox();">Show Alert</button>
        <script>
            function alertbox() {
                alert("HTML Code Play");
            }
        </script>
    </template>
    <template id="code2">
        <script>
            function showalert() {
                alert("HTML Code Play");
            }
        </script>
        <input type="button" value="Show Alert" onclick="showalert();">
    </template>
</div>
<script>
    mocha.setup('bdd');

    $(function () {
        var assert = function (expr, msg) {
            if (!expr) throw new Error(msg || 'failed');
        };
        describe("implement 'show alert button' test", function () {
            before(function () {
                window.alert = sinon.spy();
            });
            it("window.alert must to be called with 'HTML Code Play'", function (done) {

                $("#live-code-stage").html($("#code1").html()); // testing code1

                $("button, input[type='button']").click();
                assert(window.alert.calledWith("HTML Code Play"));
                done();
            });
        });
        mocha.run();
    });
</script>
</body>
</html>    

如何在浏览器上测试用户代码
如何在浏览器上测试用户代码
显示警惕
函数alertbox(){
警报(“HTML代码播放”);
}
函数showalert(){
警报(“HTML代码播放”);
}
摩卡咖啡设置(“bdd”);
$(函数(){
var assert=函数(expr,msg){
如果(!expr)抛出新错误(msg | |“failed”);
};
描述(“执行”显示警报按钮“测试”,功能(){
前(函数(){
window.alert=sinon.spy();
});
它(“必须使用“HTML代码播放”调用window.alert”,函数(完成){
$(“#实时代码阶段”).html($(“#代码1”).html();//测试代码1
$(“按钮,输入[type='button'])。单击();
断言(window.alert.calledWith(“HTML代码播放”);
完成();
});
});
mocha.run();
});
要点
  • 您可以像这样模拟“window.alert”:
    window.alert=sinon.spy()

  • 您可以检查它(=警报)是否使用以下正确参数调用:
    assert(window.alert.calledWith(“HTML代码播放”)

如果用户创建的代码错误,mocha测试将失败。
引入qunit可以改善测试报告的外观


但这只是一个样本。必须有许多选项来归档您想要的内容

它们有什么不同?浏览器模拟器怎么样?如果可以,您可以尝试CasperJS。我正在使用cordova for android应用程序。我想知道某种单元测试框架是否有助于此。@Adyson它看起来没有帮助。你能用上面的代码给出一个示例代码吗?它没有什么可操作的。@AlexKudryashev请解释更多。我认为提问者的主要目的是“为JavaScript测试创建一个应用程序”和“检查用户代码是否正确”(“在真实环境中发出警报”)。OP没有提到所提供代码的目的。因此,您无法回答未设置的问题。您的代码可能很好,但与OP无关。@hitokun_\s您可以在jsfiddle.net中创建帖子吗,