How can I verify that the value is "greater than or equal to" in Jasmine?

I want to confirm that the value is decimal (or 0), so the number must be greater than or equal to zero and less than 1.

describe('percent',function(){ it('should be a decimal', function() { var percent = insights.percent; expect(percent).toBeGreaterThan(0); expect(percent).toBeLessThan(1); }); }); 

How do I emulate "> = 0"?

+79
javascript tdd jasmine
Jun 06 '14 at 20:41
source share
10 answers

You just need to start the comparison operation first and then check if this is valid.

 describe('percent',function(){ it('should be a decimal',function(){ var percent = insights.percent; expect(percent >= 0).toBeTruthy(); expect(percent).toBeLessThan(1); }); }); 
+64
Jun 06 '14 at 20:48
source share

I thought I should update this since the API has changed in newer versions of Jasmine. The Jasmine API now has built-in functions for:

  • toBeGreaterThanOrEqual
  • toBeLessThanOrEqual

You should use these features as recommended below.

Click here for more information on the Jasmine matchers API.




I know this is an old and resolved issue, but I noticed that a rather neat solution was skipped. Since the inverse function is greater than or equal to less than, try:

 expect(percent).not.toBeLessThan(0); 

With this approach, the percent value can be returned by an asynchronous function and processed as part of the control flow.

+96
09 Sep '15 at 9:02
source share

The current version of Jasmine supports toBeGreaterThan and toBeLessThan.

 expect(myVariable).toBeGreaterThan(0); 
+11
Apr 12 '16 at 15:35
source share

Somewhat strange, this is not basic functionality.

You can add your own match as follows:

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 }; } }; } ); }; 

In fact, you define a constructor for your match - this is a function that returns a conjugation object.

Turn it on before downloading. Base sockets load at boot time.

Your html file should look like this:

 <!-- 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> 

Then in your boot.js add a call to add matches after defining jasmine, but before jasmine.getEnv (). Getting env is actually a (slightly mistakenly named) setup call.

Matches get set up in a call to setupCoreMatchers in the Env constructor.

 /** * ## Require &amp; Instantiate * * Require Jasmine core files. Specifically, this requires and attaches all of Jasmine 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(); 

They show another way to add custom matches in test cases, but the way it works is to recreate the match before each individual test using beforeEach . It seems pretty horrible, so I thought I would go this route.

+5
Jun 27 '14 at 15:26
source share

I'm late for this, but posting it in case someone still visits this question for answers, I use Jasmine version 3.0 and, as @Patrizio Rullo mentioned, you can use toBeGreaterThanOrEqual / toBeLessThanOrEqual .

It was added in version 2.5 in accordance with the release notes - https://github.com/jasmine/jasmine/blob/master/release_notes/2.5.0.md

For example,

 expect(percent).toBeGreaterThanOrEqual(1,"This is optional expect failure message"); 

or

 expect(percent).toBeGreaterThanOrEqual(1); 
+5
Feb 20 '18 at 11:09
source share

Today I faced the same problem, and as it turned out, adding a custom layout to it is difficult. The main advantage of user matching is that it can return meaningful messages when a test fails.

So, here is the code for two matches, .toBeAtLeast() and .toBeAtMost() , if that helps someone.

 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; } }; } } ); } ); 
+4
Jul 17 '15 at 12:01
source share

This was simply combined in the main branch of the Jasmine GitHub to add the helpers you need:

Add to BeGreatThanOrEqual and toBeLessThanOrEqual matches

But I have no idea which issue it will be in. At the same time, you can try to use my commit code in your local copy of Jasmine.

+4
Feb 18 '16 at 0:01
source share

I recommend using this Jasmine clip: https://github.com/JamieMason/Jasmine-Matchers

+1
Apr 6 '15 at 19:50
source share

You can use the least function to check if a value is larger or some other value.

The least alias is gte (large or equal). Conversely, you can use lte (less than or equal to) to check the opposite.

So, to answer the question, you can do:

expect(percent).to.be.gte(0)

+1
May 30 '17 at 18:07
source share

Just use the following:

 expect(percent >= 0).toBeTruthy() 
0
Jan 16 '19 at 16:32
source share



All Articles