[英]TestCafe - How to check if the value is greater than or equal in the assertion
[英]How can I test that a value is “greater than or equal to” in Jasmine?
我想確認一個值是小數(或 0),所以這個數字應該大於或等於 0並且小於 1。
describe('percent',function(){
it('should be a decimal', function() {
var percent = insights.percent;
expect(percent).toBeGreaterThan(0);
expect(percent).toBeLessThan(1);
});
});
我如何模仿“ >= 0 ”?
我想我應該更新它,因為 API 在較新版本的 Jasmine 中發生了變化。 Jasmine API 現在內置了以下功能:
您應該優先使用這些功能而不是下面的建議。
單擊此處了解有關 Jasmine 匹配器 API 的更多信息
我知道這是一個古老且已解決的問題,但我注意到錯過了一個相當簡潔的解決方案。 由於大於或等於是小於函數的反函數,請嘗試:
expect(percent).not.toBeLessThan(0);
在這種方法中,百分比值可以由異步函數返回並作為控制流的一部分進行處理。
您只需要先運行比較操作,然后檢查它是否為真。
describe('percent',function(){
it('should be a decimal',function(){
var percent = insights.percent;
expect(percent >= 0).toBeTruthy();
expect(percent).toBeLessThan(1);
});
});
Jasmine 的當前版本支持 toBeGreaterThan 和 toBeLessThan。
expect(myVariable).toBeGreaterThan(0);
我遲到了,但發布它以防萬一有人仍然訪問這個問題尋找答案,我使用的是 Jasmine 3.0 版,正如@Patrizio Rullo 所提到的,您可以使用toBeGreaterThanOrEqual/toBeLessThanOrEqual 。
它是根據發行說明在 2.5 版中添加的 - https://github.com/jasmine/jasmine/blob/master/release_notes/2.5.0.md
例如
expect(percent).toBeGreaterThanOrEqual(1,"This is optional expect failure message");
或者
expect(percent).toBeGreaterThanOrEqual(1);
有點奇怪,這不是基本功能
您可以像這樣添加自定義匹配器:
JasmineExtensions.js
yourGlobal.addExtraMatchers = function () {
var addMatcher = function (name, func) {
func.name = name;
jasmine.matchers[name] = func;
};
addMatcher("toBeGreaterThanOrEqualTo", function () {
return {
compare: function (actual, expected) {
return {
pass: actual >= expected
};
}
};
}
);
};
實際上,您正在為匹配器定義一個構造函數 - 它是一個返回匹配器對象的函數。
在“啟動”之前包括它。 基本匹配器在啟動時加載。
您的 html 文件應如下所示:
<!-- jasmine test framework-->
<script type="text/javascript" src="lib/jasmine-2.0.0/jasmine.js"></script>
<script type="text/javascript" src="lib/jasmine-2.0.0/jasmine-html.js"></script>
<!-- custom matchers -->
<script type="text/javascript" src="Tests/JasmineExtensions.js"></script>
<!-- initialisation-->
<script type="text/javascript" src="lib/jasmine-2.0.0/boot.js"></script>
然后在您的 boot.js 中添加調用以在定義 jasmine 之后但在 jasmine.getEnv() 之前添加匹配器。 Get env 實際上是一個(有點誤導性的命名)設置調用。
匹配器在 Env 構造函數中調用 setupCoreMatchers 中設置。
/**
* ## Require & Instantiate
*
* Require Jasmine's core files. Specifically, this requires and attaches all of Jasmine's code to the `jasmine` reference.
*/
window.jasmine = jasmineRequire.core(jasmineRequire);
yourGlobal.addExtraMatchers();
/**
* Since this is being run in a browser and the results should populate to an HTML page, require the HTML-specific Jasmine code, injecting the same reference.
*/
jasmineRequire.html(jasmine);
/**
* Create the Jasmine environment. This is used to run all specs in a project.
*/
var env = jasmine.getEnv();
他們展示了在示例測試中添加自定義匹配器的另一種方法,但是它的工作方式是在每次測試之前使用beforeEach
重新創建匹配器。 這看起來很可怕,所以我想我會采用這種方法。
我今天遇到了同樣的問題,事實證明,為其添加自定義匹配器並不難。 自定義匹配器的主要優點是它可以在測試失敗時返回有意義的消息。
所以這里是兩個匹配器的代碼, .toBeAtLeast()
和.toBeAtMost()
,以防它對某人有幫助。
beforeEach( function () {
// When beforeEach is called outside of a `describe` scope, the matchers are
// available globally. See http://stackoverflow.com/a/11942151/508355
jasmine.addMatchers( {
toBeAtLeast: function () {
return {
compare: function ( actual, expected ) {
var result = {};
result.pass = actual >= expected;
if ( result.pass ) {
result.message = "Expected " + actual + " to be less than " + expected;
} else {
result.message = "Expected " + actual + " to be at least " + expected;
}
return result;
}
};
},
toBeAtMost: function () {
return {
compare: function ( actual, expected ) {
var result = {};
result.pass = actual <= expected;
if ( result.pass ) {
result.message = "Expected " + actual + " to be greater than " + expected;
} else {
result.message = "Expected " + actual + " to be at most " + expected;
}
return result;
}
};
}
} );
} );
它剛剛合並到 Jasmine GitHub master 分支 my patch 中以添加您需要的匹配器:
添加 toBeGreatThanOrEqual 和 toBeLessThanOrEqual 匹配器
但我不知道它將在哪個版本中發布。 同時,您可以嘗試在您的本地 Jasmine 副本中使用我提交的代碼。
我推薦使用這個 Jasmine 插件: https : //github.com/JamieMason/Jasmine-Matchers
您可以使用least
函數檢查某個值是否大於或等於某個其他值。
least
的別名是gte
(大於或等於)。 反之亦然,您可以使用lte
(小於或等於)來檢查相反的情況。
所以,要回答這個問題,你可以這樣做:
expect(percent).to.be.gte(0)
使用這個更新的公式:
大於或等於小於或等於
應該管用!
我很驚訝上面所有的人都浪費了這么多時間來嘗試“以茉莉花方式”,而多年來茉莉花匹配器被證明是不方便的,只涵蓋基本情況。
添加自定義匹配器的答案更糟糕,因為它會讓未來加入您團隊的新開發人員感到驚訝,可能除了您之外沒有其他人會使用它。
為什么不簡單
describe('percent',function(){
it('should be a decimal', function() {
var percent = insights.percent;
expect(percent>=0 && percent<1).toBeTruthy();
});
});
繼續前進,做一些有用的事情? 測試用例名稱已經告訴你應該是多少百分比。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.