javascript Jasmine 测试中条件语句的使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22570046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use of conditional statements in Jasmine tests
提问by redrum
I have two functions I want to test; one that runs only in "modern browsers" the other that runs only in "older" browsers.
我有两个要测试的功能;一个仅在“现代浏览器”中运行,另一个仅在“旧”浏览器中运行。
I am using Jasmine as my testing framework and Karma to run my tests. Karma will launch many browsers in which all the tests are run.
我使用 Jasmine 作为我的测试框架和 Karma 来运行我的测试。Karma 将启动许多运行所有测试的浏览器。
My problemis that testing ALL my functions in ALL browsers will lead some tests to fail. For example, testing a function that should run only in modern browsers will fail in when tested in IE8.
我的问题是在所有浏览器中测试我的所有功能会导致一些测试失败。例如,测试一个只应在现代浏览器中运行的功能在 IE8 中测试时将失败。
Code to test:
测试代码:
function getStuffFromSmartBrowser() {
return 'foo';
}
function getStuffFromNotSoSmartBrowser() {
return 'bar';
}
if ('intelligence' in browser) {
getStuffFromSmartBrowser();
} else {
getStuffFromNotSoSmartBrowser();
}
Original test code:
原始测试代码:
describe("A suite", function () {
it("for smart browsers", function () {
expect(getStuffFromSmartBrowser()).toEqual('foo');
});
});
describe("A suite", function () {
it("for not so smart browsers", function () {
expect(getStuffFromNotSoSmartBrowser()).toEqual('bar');
});
});
In these test cases, one test will fail in one type of browsers while the other test will fail in the other type of browsers.
在这些测试用例中,一个测试将在一种类型的浏览器中失败,而另一个测试将在另一种类型的浏览器中失败。
Alternative test code:
替代测试代码:
describe("A suite", function () {
if ('intelligence' in browser) {
it("for smart browsers", function () {
expect(getStuffFromSmartBrowser()).toEqual('foo');
});
} else {
it("for not so smart browsers", function () {
expect(getStuffFromNotSoSmartBrowser()).toEqual('bar');
});
}
});
The alternative test code run perfectly well in all tested browsers.
替代测试代码在所有经过测试的浏览器中都运行良好。
Questions:
问题:
A - Is it good practice to use conditional statements in tests?
A - 在测试中使用条件语句是一种好习惯吗?
B - If not, what would be a better approach?
B - 如果没有,什么是更好的方法?
采纳答案by Eric
It's generally considered bad practice to have conditionals insidethe test code. Outside of the test code is slightly more acceptable and, in the absence of a built-in mechanism to select test specs by browser, your solution is probably the simplest. However, you are coupling your unit tests to the browser-specific implementations.
它通常被认为是不好的做法,具有条件语句中的测试代码。在测试代码之外稍微更容易接受,并且在没有通过浏览器选择测试规范的内置机制的情况下,您的解决方案可能是最简单的。但是,您将单元测试耦合到特定于浏览器的实现。
A "better" approach would be to hide your browser-specific code behind a single function and use feature detectionto perform multiple dispatch.
“更好”的方法是将特定于浏览器的代码隐藏在单个函数后面,并使用特征检测来执行多个 dispatch。
function getStuff() {
if (browserIsSmart()) {
return getStuffFromSmartBrowser();
}
else {
return getStuffFromNotSoSmartBrowser();
}
}
function browserIsSmart() {
return ('intelligence' in browser); // Or whatever
}
Here's where it gets messy. Your two functions have different return values. Ideally you'll want the same return value for all related functions, but this isn't always simple. For example, textarea selection in IE8 uses completely different objects than textarea selection in modern browsers. Sometimes you can hide the details (e.g. using the Strategy Pattern), sometimes you can't.
这就是它变得混乱的地方。您的两个函数具有不同的返回值。理想情况下,您希望所有相关函数都具有相同的返回值,但这并不总是那么简单。例如,IE8 中的 textarea 选择使用与现代浏览器中的 textarea 选择完全不同的对象。有时您可以隐藏细节(例如使用Strategy Pattern),有时则不能。
If you can't make it clean, make it obvious.
如果你不能把它弄干净,那就让它显而易见。