2009-05-29 03:02:15 +00:00
describe ( "jasmine.Matchers" , function ( ) {
2009-12-24 22:01:13 +00:00
var env , spec ;
2009-10-30 00:03:24 +00:00
2009-05-29 03:02:15 +00:00
beforeEach ( function ( ) {
env = new jasmine . Env ( ) ;
2009-10-06 05:36:10 +00:00
env . updateInterval = 0 ;
2009-12-24 22:01:13 +00:00
var suite = env . describe ( "suite" , function ( ) {
spec = env . it ( "spec" , function ( ) {
} ) ;
} ) ;
spyOn ( spec , 'addMatcherResult' ) ;
2010-08-04 20:52:38 +00:00
this . addMatchers ( {
toPass : function ( ) {
return lastResult ( ) . passed ( ) ;
} ,
toFail : function ( ) {
return ! lastResult ( ) . passed ( ) ;
}
} )
2009-05-29 03:02:15 +00:00
} ) ;
2009-10-30 00:03:24 +00:00
function match ( value ) {
2009-12-24 22:01:13 +00:00
return spec . expect ( value ) ;
}
2010-06-24 17:34:03 +00:00
2009-12-24 22:01:13 +00:00
function lastResult ( ) {
return spec . addMatcherResult . mostRecentCall . args [ 0 ] ;
2009-05-29 03:02:15 +00:00
}
2010-08-04 01:51:31 +00:00
function catchException ( fn ) {
try {
fn . call ( ) ;
} catch ( e ) {
return e ;
}
throw new Error ( "expected function to throw an exception" ) ;
}
2010-06-22 23:22:09 +00:00
it ( "toEqual with primitives, objects, dates, etc." , function ( ) {
2010-08-04 20:52:38 +00:00
expect ( match ( true ) . toEqual ( true ) ) . toPass ( ) ;
2009-05-29 03:02:15 +00:00
2010-08-04 20:52:38 +00:00
expect ( match ( { foo : 'bar' } ) . toEqual ( null ) ) . toFail ( ) ;
2009-05-29 03:02:15 +00:00
2009-10-30 00:03:24 +00:00
var functionA = function ( ) {
return 'hi' ;
} ;
var functionB = function ( ) {
return 'hi' ;
} ;
2010-08-04 20:52:38 +00:00
expect ( match ( { foo : functionA } ) . toEqual ( { foo : functionB } ) ) . toFail ( ) ;
expect ( match ( { foo : functionA } ) . toEqual ( { foo : functionA } ) ) . toPass ( ) ;
2009-05-29 03:02:15 +00:00
2010-08-04 20:52:38 +00:00
expect ( ( match ( false ) . toEqual ( true ) ) ) . toFail ( ) ;
2009-05-29 03:02:15 +00:00
var circularGraph = { } ;
circularGraph . referenceToSelf = circularGraph ;
2010-08-04 20:52:38 +00:00
expect ( ( match ( circularGraph ) . toEqual ( circularGraph ) ) ) . toPass ( ) ;
2009-05-29 03:02:15 +00:00
2010-08-04 20:52:38 +00:00
expect ( ( match ( new Date ( 2008 , 1 , 3 , 15 , 17 , 19 , 1234 ) ) . toEqual ( new Date ( 2009 , 1 , 3 , 15 , 17 , 19 , 1234 ) ) ) ) . toFail ( ) ;
expect ( ( match ( new Date ( 2008 , 1 , 3 , 15 , 17 , 19 , 1234 ) ) . toEqual ( new Date ( 2008 , 1 , 3 , 15 , 17 , 19 , 1234 ) ) ) ) . toPass ( ) ;
2009-05-29 03:02:15 +00:00
2010-08-04 20:52:38 +00:00
expect ( match ( true ) . toNotEqual ( false ) ) . toPass ( ) ;
expect ( ( match ( true ) . toNotEqual ( true ) ) ) . toFail ( ) ;
2009-05-29 03:02:15 +00:00
2010-08-04 20:52:38 +00:00
expect ( ( match ( [ 'a' , 'b' ] ) . toEqual ( [ 'a' , jasmine . undefined ] ) ) ) . toFail ( ) ;
expect ( ( match ( [ 'a' , 'b' ] ) . toEqual ( [ 'a' , 'b' , jasmine . undefined ] ) ) ) . toFail ( ) ;
2010-03-17 15:19:29 +00:00
2010-08-04 20:52:38 +00:00
expect ( ( match ( new String ( "cat" ) ) . toEqual ( "cat" ) ) ) . toPass ( ) ;
expect ( ( match ( new String ( "cat" ) ) . toNotEqual ( "cat" ) ) ) . toFail ( ) ;
2010-03-19 03:32:40 +00:00
2010-08-04 20:52:38 +00:00
expect ( ( match ( new Number ( 5 ) ) . toEqual ( 5 ) ) ) . toPass ( ) ;
expect ( ( match ( new Number ( '5' ) ) . toEqual ( 5 ) ) ) . toPass ( ) ;
expect ( ( match ( new Number ( 5 ) ) . toNotEqual ( 5 ) ) ) . toFail ( ) ;
expect ( ( match ( new Number ( '5' ) ) . toNotEqual ( 5 ) ) ) . toFail ( ) ;
2009-05-29 03:02:15 +00:00
} ) ;
2010-06-22 23:22:09 +00:00
it ( "toEqual with DOM nodes" , function ( ) {
var nodeA = document . createElement ( 'div' ) ;
var nodeB = document . createElement ( 'div' ) ;
2010-08-04 20:52:38 +00:00
expect ( ( match ( nodeA ) . toEqual ( nodeA ) ) ) . toPass ( ) ;
expect ( ( match ( nodeA ) . toEqual ( nodeB ) ) ) . toFail ( ) ;
2010-06-22 23:22:09 +00:00
} ) ;
2009-10-30 00:03:24 +00:00
it ( "toEqual to build an Expectation Result" , function ( ) {
2009-10-30 03:33:01 +00:00
var actual = 'a' ;
var matcher = match ( actual ) ;
var expected = 'b' ;
matcher . toEqual ( expected ) ;
2009-10-30 00:03:24 +00:00
2009-12-24 22:01:13 +00:00
var result = lastResult ( ) ;
2009-10-30 00:03:24 +00:00
expect ( result . matcherName ) . toEqual ( "toEqual" ) ;
2010-08-04 20:52:38 +00:00
expect ( result . passed ( ) ) . toFail ( ) ;
2009-10-30 03:33:01 +00:00
expect ( result . message ) . toMatch ( jasmine . pp ( actual ) ) ;
expect ( result . message ) . toMatch ( jasmine . pp ( expected ) ) ;
expect ( result . expected ) . toEqual ( expected ) ;
expect ( result . actual ) . toEqual ( actual ) ;
2009-10-30 00:03:24 +00:00
} ) ;
it ( "toNotEqual to build an Expectation Result" , function ( ) {
2009-10-30 03:33:01 +00:00
var str = 'a' ;
var matcher = match ( str ) ;
matcher . toNotEqual ( str ) ;
2009-10-30 00:03:24 +00:00
2009-12-24 22:01:13 +00:00
var result = lastResult ( ) ;
2009-10-30 00:03:24 +00:00
expect ( result . matcherName ) . toEqual ( "toNotEqual" ) ;
2010-08-04 20:52:38 +00:00
expect ( result . passed ( ) ) . toFail ( ) ;
2009-10-30 03:33:01 +00:00
expect ( result . message ) . toMatch ( jasmine . pp ( str ) ) ;
expect ( result . message ) . toMatch ( 'not' ) ;
expect ( result . expected ) . toEqual ( str ) ;
expect ( result . actual ) . toEqual ( str ) ;
2009-10-30 00:03:24 +00:00
} ) ;
2009-05-29 03:02:15 +00:00
it ( 'toBe should return true only if the expected and actual items === each other' , function ( ) {
var a = { } ;
var b = { } ;
//noinspection UnnecessaryLocalVariableJS
var c = a ;
2010-08-04 20:52:38 +00:00
expect ( ( match ( a ) . toBe ( b ) ) ) . toFail ( ) ;
expect ( ( match ( a ) . toBe ( a ) ) ) . toPass ( ) ;
expect ( ( match ( a ) . toBe ( c ) ) ) . toPass ( ) ;
expect ( ( match ( a ) . toNotBe ( b ) ) ) . toPass ( ) ;
expect ( ( match ( a ) . toNotBe ( a ) ) ) . toFail ( ) ;
expect ( ( match ( a ) . toNotBe ( c ) ) ) . toFail ( ) ;
2009-05-29 03:02:15 +00:00
} ) ;
2009-10-30 00:03:24 +00:00
it ( "toBe to build an ExpectationResult" , function ( ) {
var expected = 'b' ;
var actual = 'a' ;
var matcher = match ( actual ) ;
matcher . toBe ( expected ) ;
2009-12-24 22:01:13 +00:00
var result = lastResult ( ) ;
2009-10-30 00:03:24 +00:00
expect ( result . matcherName ) . toEqual ( "toBe" ) ;
2010-08-04 20:52:38 +00:00
expect ( result . passed ( ) ) . toFail ( ) ;
2009-10-30 00:03:24 +00:00
expect ( result . message ) . toMatch ( jasmine . pp ( actual ) ) ;
expect ( result . message ) . toMatch ( jasmine . pp ( expected ) ) ;
2009-10-30 03:33:01 +00:00
expect ( result . expected ) . toEqual ( expected ) ;
2009-10-30 00:03:24 +00:00
expect ( result . actual ) . toEqual ( actual ) ;
} ) ;
it ( "toNotBe to build an ExpectationResult" , function ( ) {
var str = 'a' ;
var matcher = match ( str ) ;
matcher . toNotBe ( str ) ;
2009-12-24 22:01:13 +00:00
var result = lastResult ( ) ;
2009-10-30 00:03:24 +00:00
expect ( result . matcherName ) . toEqual ( "toNotBe" ) ;
2010-08-04 20:52:38 +00:00
expect ( result . passed ( ) ) . toFail ( ) ;
2009-10-30 00:03:24 +00:00
expect ( result . message ) . toMatch ( str ) ;
2009-10-30 03:33:01 +00:00
expect ( result . expected ) . toEqual ( str ) ;
2009-10-30 00:03:24 +00:00
expect ( result . actual ) . toEqual ( str ) ;
} ) ;
2009-05-29 03:02:15 +00:00
it ( "toMatch and #toNotMatch should perform regular expression matching on strings" , function ( ) {
2010-08-04 20:52:38 +00:00
expect ( ( match ( 'foobarbel' ) . toMatch ( /bar/ ) ) ) . toPass ( ) ;
expect ( ( match ( 'foobazbel' ) . toMatch ( /bar/ ) ) ) . toFail ( ) ;
2009-05-29 03:02:15 +00:00
2010-08-04 20:52:38 +00:00
expect ( ( match ( 'foobarbel' ) . toMatch ( "bar" ) ) ) . toPass ( ) ;
expect ( ( match ( 'foobazbel' ) . toMatch ( "bar" ) ) ) . toFail ( ) ;
2009-05-29 03:02:15 +00:00
2010-08-04 20:52:38 +00:00
expect ( ( match ( 'foobarbel' ) . toNotMatch ( /bar/ ) ) ) . toFail ( ) ;
expect ( ( match ( 'foobazbel' ) . toNotMatch ( /bar/ ) ) ) . toPass ( ) ;
2009-05-29 03:02:15 +00:00
2010-08-04 20:52:38 +00:00
expect ( ( match ( 'foobarbel' ) . toNotMatch ( "bar" ) ) ) . toFail ( ) ;
expect ( ( match ( 'foobazbel' ) . toNotMatch ( "bar" ) ) ) . toPass ( ) ;
2009-05-29 03:02:15 +00:00
} ) ;
2009-10-30 00:03:24 +00:00
it ( "toMatch w/ RegExp to build an ExpectationResult" , function ( ) {
2009-10-30 03:33:01 +00:00
var actual = 'a' ;
var matcher = match ( actual ) ;
var expected = /b/ ;
matcher . toMatch ( expected ) ;
2009-10-30 00:03:24 +00:00
2009-12-24 22:01:13 +00:00
var result = lastResult ( ) ;
2009-10-30 00:03:24 +00:00
expect ( result . matcherName ) . toEqual ( "toMatch" ) ;
2010-08-04 20:52:38 +00:00
expect ( result . passed ( ) ) . toFail ( ) ;
2009-10-30 03:33:01 +00:00
expect ( result . message ) . toMatch ( jasmine . pp ( actual ) ) ;
expect ( result . message ) . toMatch ( expected . toString ( ) ) ;
expect ( result . expected ) . toEqual ( expected ) ;
expect ( result . actual ) . toEqual ( actual ) ;
2009-10-30 00:03:24 +00:00
} ) ;
it ( "toMatch w/ String to build an ExpectationResult" , function ( ) {
2009-10-30 03:33:01 +00:00
var actual = 'a' ;
var matcher = match ( actual ) ;
var expected = 'b' ;
matcher . toMatch ( expected ) ;
2009-10-30 00:03:24 +00:00
2009-12-24 22:01:13 +00:00
var result = lastResult ( ) ;
2009-10-30 00:03:24 +00:00
expect ( result . matcherName ) . toEqual ( "toMatch" ) ;
2010-08-04 20:52:38 +00:00
expect ( result . passed ( ) ) . toFail ( ) ;
2009-11-12 20:43:49 +00:00
expect ( result . message ) . toEqual ( "Expected 'a' to match 'b'." ) ;
2009-10-30 03:33:01 +00:00
expect ( result . expected ) . toEqual ( expected ) ;
expect ( result . actual ) . toEqual ( actual ) ;
2009-10-30 00:03:24 +00:00
} ) ;
it ( "toNotMatch w/ RegExp to build an ExpectationResult" , function ( ) {
2009-10-30 03:33:01 +00:00
var actual = 'a' ;
var matcher = match ( actual ) ;
var expected = /a/ ;
matcher . toNotMatch ( expected ) ;
2009-10-30 00:03:24 +00:00
2009-12-24 22:01:13 +00:00
var result = lastResult ( ) ;
2009-10-30 00:03:24 +00:00
expect ( result . matcherName ) . toEqual ( "toNotMatch" ) ;
2010-08-04 20:52:38 +00:00
expect ( result . passed ( ) ) . toFail ( ) ;
2009-11-12 20:43:49 +00:00
expect ( result . message ) . toEqual ( "Expected 'a' to not match /a/." ) ;
2009-10-30 03:33:01 +00:00
expect ( result . expected ) . toEqual ( expected ) ;
expect ( result . actual ) . toEqual ( actual ) ;
2009-10-30 00:03:24 +00:00
} ) ;
it ( "toNotMatch w/ String to build an ExpectationResult" , function ( ) {
2009-10-30 03:33:01 +00:00
var str = 'a' ;
var matcher = match ( str ) ;
matcher . toNotMatch ( str ) ;
2009-10-30 00:03:24 +00:00
2009-12-24 22:01:13 +00:00
var result = lastResult ( ) ;
2009-10-30 00:03:24 +00:00
expect ( result . matcherName ) . toEqual ( "toNotMatch" ) ;
2010-08-04 20:52:38 +00:00
expect ( result . passed ( ) ) . toFail ( ) ;
2009-11-12 20:43:49 +00:00
expect ( result . message ) . toEqual ( "Expected 'a' to not match 'a'." ) ;
2009-10-30 03:33:01 +00:00
expect ( result . expected ) . toEqual ( str ) ;
expect ( result . actual ) . toEqual ( str ) ;
2009-10-30 00:03:24 +00:00
} ) ;
2009-05-29 03:02:15 +00:00
it ( "toBeDefined" , function ( ) {
2010-08-04 20:52:38 +00:00
expect ( match ( 'foo' ) . toBeDefined ( ) ) . toPass ( ) ;
expect ( match ( jasmine . undefined ) . toBeDefined ( ) ) . toFail ( ) ;
2009-05-29 03:02:15 +00:00
} ) ;
2009-10-30 00:03:24 +00:00
it ( "toBeDefined to build an ExpectationResult" , function ( ) {
2009-11-26 16:12:06 +00:00
var matcher = match ( jasmine . undefined ) ;
2009-10-30 00:03:24 +00:00
matcher . toBeDefined ( ) ;
2009-12-24 22:01:13 +00:00
var result = lastResult ( ) ;
2009-10-30 00:03:24 +00:00
expect ( result . matcherName ) . toEqual ( "toBeDefined" ) ;
2010-08-04 20:52:38 +00:00
expect ( result . passed ( ) ) . toFail ( ) ;
2009-11-12 19:36:58 +00:00
expect ( result . message ) . toEqual ( 'Expected undefined to be defined.' ) ;
2009-11-26 16:12:06 +00:00
expect ( result . actual ) . toEqual ( jasmine . undefined ) ;
2009-10-30 00:03:24 +00:00
} ) ;
it ( "toBeUndefined" , function ( ) {
2010-08-04 20:52:38 +00:00
expect ( match ( 'foo' ) . toBeUndefined ( ) ) . toFail ( ) ;
expect ( match ( jasmine . undefined ) . toBeUndefined ( ) ) . toPass ( ) ;
2009-10-30 00:03:24 +00:00
} ) ;
2009-05-29 03:02:15 +00:00
it ( "toBeNull" , function ( ) {
2010-08-04 20:52:38 +00:00
expect ( match ( null ) . toBeNull ( ) ) . toPass ( ) ;
expect ( match ( jasmine . undefined ) . toBeNull ( ) ) . toFail ( ) ;
expect ( match ( "foo" ) . toBeNull ( ) ) . toFail ( ) ;
2009-05-29 03:02:15 +00:00
} ) ;
2009-10-30 00:03:24 +00:00
it ( "toBeNull w/ String to build an ExpectationResult" , function ( ) {
var actual = 'a' ;
var matcher = match ( actual ) ;
matcher . toBeNull ( ) ;
2009-12-24 22:01:13 +00:00
var result = lastResult ( ) ;
2009-10-30 00:03:24 +00:00
expect ( result . matcherName ) . toEqual ( "toBeNull" ) ;
2010-08-04 20:52:38 +00:00
expect ( result . passed ( ) ) . toFail ( ) ;
2009-10-30 00:03:24 +00:00
expect ( result . message ) . toMatch ( jasmine . pp ( actual ) ) ;
expect ( result . message ) . toMatch ( 'null' ) ;
expect ( result . actual ) . toEqual ( actual ) ;
} ) ;
it ( "toBeNull w/ Object to build an ExpectationResult" , function ( ) {
var actual = { a : 'b' } ;
var matcher = match ( actual ) ;
matcher . toBeNull ( ) ;
2009-12-24 22:01:13 +00:00
var result = lastResult ( ) ;
2009-10-30 00:03:24 +00:00
expect ( result . matcherName ) . toEqual ( "toBeNull" ) ;
2010-08-04 20:52:38 +00:00
expect ( result . passed ( ) ) . toFail ( ) ;
2009-10-30 00:03:24 +00:00
expect ( result . message ) . toMatch ( jasmine . pp ( actual ) ) ;
expect ( result . message ) . toMatch ( 'null' ) ;
expect ( result . actual ) . toEqual ( actual ) ;
} ) ;
2009-05-29 03:02:15 +00:00
it ( "toBeFalsy" , function ( ) {
2010-08-04 20:52:38 +00:00
expect ( match ( false ) . toBeFalsy ( ) ) . toPass ( ) ;
expect ( match ( true ) . toBeFalsy ( ) ) . toFail ( ) ;
expect ( match ( jasmine . undefined ) . toBeFalsy ( ) ) . toPass ( ) ;
expect ( match ( 0 ) . toBeFalsy ( ) ) . toPass ( ) ;
expect ( match ( "" ) . toBeFalsy ( ) ) . toPass ( ) ;
2009-05-29 03:02:15 +00:00
} ) ;
2009-10-30 00:03:24 +00:00
it ( "toBeFalsy to build an ExpectationResult" , function ( ) {
var actual = 'a' ;
var matcher = match ( actual ) ;
matcher . toBeFalsy ( ) ;
2009-12-24 22:01:13 +00:00
var result = lastResult ( ) ;
2009-10-30 00:03:24 +00:00
expect ( result . matcherName ) . toEqual ( "toBeFalsy" ) ;
2010-08-04 20:52:38 +00:00
expect ( result . passed ( ) ) . toFail ( ) ;
2009-10-30 00:03:24 +00:00
expect ( result . message ) . toMatch ( jasmine . pp ( actual ) ) ;
expect ( result . message ) . toMatch ( 'falsy' ) ;
expect ( result . actual ) . toEqual ( actual ) ;
} ) ;
2009-05-29 03:02:15 +00:00
it ( "toBeTruthy" , function ( ) {
2010-08-04 20:52:38 +00:00
expect ( match ( false ) . toBeTruthy ( ) ) . toFail ( ) ;
expect ( match ( true ) . toBeTruthy ( ) ) . toPass ( ) ;
expect ( match ( jasmine . undefined ) . toBeTruthy ( ) ) . toFail ( ) ;
expect ( match ( 0 ) . toBeTruthy ( ) ) . toFail ( ) ;
expect ( match ( "" ) . toBeTruthy ( ) ) . toFail ( ) ;
expect ( match ( "hi" ) . toBeTruthy ( ) ) . toPass ( ) ;
expect ( match ( 5 ) . toBeTruthy ( ) ) . toPass ( ) ;
expect ( match ( { foo : 1 } ) . toBeTruthy ( ) ) . toPass ( ) ;
2009-05-29 03:02:15 +00:00
} ) ;
2009-10-30 00:03:24 +00:00
it ( "toBeTruthy to build an ExpectationResult" , function ( ) {
var matcher = match ( false ) ;
matcher . toBeTruthy ( ) ;
2009-12-24 22:01:13 +00:00
var result = lastResult ( ) ;
2009-10-30 00:03:24 +00:00
expect ( result . matcherName ) . toEqual ( "toBeTruthy" ) ;
2010-08-04 20:52:38 +00:00
expect ( result . passed ( ) ) . toFail ( ) ;
2009-11-12 19:36:58 +00:00
expect ( result . message ) . toEqual ( "Expected false to be truthy." ) ;
2010-08-04 20:52:38 +00:00
expect ( result . actual ) . toFail ( ) ;
2009-10-30 00:03:24 +00:00
} ) ;
2009-05-29 03:02:15 +00:00
it ( "toEqual" , function ( ) {
2010-08-04 20:52:38 +00:00
expect ( match ( jasmine . undefined ) . toEqual ( jasmine . undefined ) ) . toPass ( ) ;
expect ( match ( { foo : 'bar' } ) . toEqual ( { foo : 'bar' } ) ) . toPass ( ) ;
expect ( match ( "foo" ) . toEqual ( { bar : jasmine . undefined } ) ) . toFail ( ) ;
expect ( match ( { foo : jasmine . undefined } ) . toEqual ( "goo" ) ) . toFail ( ) ;
expect ( match ( { foo : { bar : jasmine . undefined } } ) . toEqual ( "goo" ) ) . toFail ( ) ;
2009-05-29 03:02:15 +00:00
} ) ;
it ( "toEqual with jasmine.any()" , function ( ) {
2010-08-04 20:52:38 +00:00
expect ( match ( "foo" ) . toEqual ( jasmine . any ( String ) ) ) . toPass ( ) ;
expect ( match ( 3 ) . toEqual ( jasmine . any ( Number ) ) ) . toPass ( ) ;
expect ( match ( "foo" ) . toEqual ( jasmine . any ( Function ) ) ) . toFail ( ) ;
expect ( match ( "foo" ) . toEqual ( jasmine . any ( Object ) ) ) . toFail ( ) ;
expect ( match ( { someObj : 'foo' } ) . toEqual ( jasmine . any ( Object ) ) ) . toPass ( ) ;
expect ( match ( { someObj : 'foo' } ) . toEqual ( jasmine . any ( Function ) ) ) . toFail ( ) ;
2009-10-30 00:03:24 +00:00
expect ( match ( function ( ) {
2010-08-04 20:52:38 +00:00
} ) . toEqual ( jasmine . any ( Object ) ) ) . toFail ( ) ;
expect ( match ( [ "foo" , "goo" ] ) . toEqual ( [ "foo" , jasmine . any ( String ) ] ) ) . toPass ( ) ;
2009-10-30 00:03:24 +00:00
expect ( match ( function ( ) {
2010-08-04 20:52:38 +00:00
} ) . toEqual ( jasmine . any ( Function ) ) ) . toPass ( ) ;
2009-10-30 00:03:24 +00:00
expect ( match ( [ "a" , function ( ) {
2010-08-04 20:52:38 +00:00
} ] ) . toEqual ( [ "a" , jasmine . any ( Function ) ] ) ) . toPass ( ) ;
2009-05-29 03:02:15 +00:00
} ) ;
it ( "toEqual handles circular objects ok" , function ( ) {
2010-08-04 20:52:38 +00:00
expect ( match ( { foo : "bar" , baz : jasmine . undefined } ) . toEqual ( { foo : "bar" , baz : jasmine . undefined } ) ) . toPass ( ) ;
expect ( match ( { foo : [ 'bar' , 'baz' , 'quux' ] } ) . toEqual ( { foo : [ 'bar' , 'baz' , 'quux' ] } ) ) . toPass ( ) ;
expect ( match ( { foo : { bar : 'baz' } , quux : 'corge' } ) . toEqual ( { foo : { bar : 'baz' } , quux : 'corge' } ) ) . toPass ( ) ;
2009-05-29 03:02:15 +00:00
var circularObject = { } ;
var secondCircularObject = { } ;
circularObject . field = circularObject ;
secondCircularObject . field = secondCircularObject ;
2010-08-04 20:52:38 +00:00
expect ( match ( circularObject ) . toEqual ( secondCircularObject ) ) . toPass ( ) ;
2009-05-29 03:02:15 +00:00
} ) ;
it ( "toNotEqual as slightly surprising behavior, but is it intentional?" , function ( ) {
2010-08-04 20:52:38 +00:00
expect ( match ( { x : "x" , y : "y" , z : "w" } ) . toNotEqual ( { x : "x" , y : "y" , z : "z" } ) ) . toPass ( ) ;
expect ( match ( { x : "x" , y : "y" , w : "z" } ) . toNotEqual ( { x : "x" , y : "y" , z : "z" } ) ) . toPass ( ) ;
expect ( match ( { x : "x" , y : "y" , z : "z" } ) . toNotEqual ( { w : "w" , x : "x" , y : "y" , z : "z" } ) ) . toPass ( ) ;
expect ( match ( { w : "w" , x : "x" , y : "y" , z : "z" } ) . toNotEqual ( { x : "x" , y : "y" , z : "z" } ) ) . toPass ( ) ;
2009-05-29 03:02:15 +00:00
} ) ;
it ( "toEqual handles arrays" , function ( ) {
2010-08-04 20:52:38 +00:00
expect ( match ( [ 1 , "A" ] ) . toEqual ( [ 1 , "A" ] ) ) . toPass ( ) ;
2009-05-29 03:02:15 +00:00
} ) ;
it ( "toContain and toNotContain" , function ( ) {
2010-08-04 20:52:38 +00:00
expect ( match ( 'ABC' ) . toContain ( 'A' ) ) . toPass ( ) ;
expect ( match ( 'ABC' ) . toContain ( 'X' ) ) . toFail ( ) ;
2009-10-30 00:03:24 +00:00
2010-08-04 20:52:38 +00:00
expect ( match ( [ 'A' , 'B' , 'C' ] ) . toContain ( 'A' ) ) . toPass ( ) ;
expect ( match ( [ 'A' , 'B' , 'C' ] ) . toContain ( 'F' ) ) . toFail ( ) ;
expect ( match ( [ 'A' , 'B' , 'C' ] ) . toNotContain ( 'F' ) ) . toPass ( ) ;
expect ( match ( [ 'A' , 'B' , 'C' ] ) . toNotContain ( 'A' ) ) . toFail ( ) ;
2009-05-29 03:02:15 +00:00
2010-08-04 20:52:38 +00:00
expect ( match ( [ 'A' , { some : 'object' } , 'C' ] ) . toContain ( { some : 'object' } ) ) . toPass ( ) ;
expect ( match ( [ 'A' , { some : 'object' } , 'C' ] ) . toContain ( { some : 'other object' } ) ) . toFail ( ) ;
2009-10-30 00:03:24 +00:00
} ) ;
2009-05-29 03:02:15 +00:00
2009-10-30 00:03:24 +00:00
it ( "toContain to build an ExpectationResult" , function ( ) {
var actual = [ 'a' , 'b' , 'c' ] ;
var matcher = match ( actual ) ;
var expected = 'x' ;
matcher . toContain ( expected ) ;
2009-12-24 22:01:13 +00:00
var result = lastResult ( ) ;
2009-10-30 00:03:24 +00:00
expect ( result . matcherName ) . toEqual ( "toContain" ) ;
2010-08-04 20:52:38 +00:00
expect ( result . passed ( ) ) . toFail ( ) ;
2009-10-30 00:03:24 +00:00
expect ( result . message ) . toMatch ( jasmine . pp ( actual ) ) ;
expect ( result . message ) . toMatch ( 'contain' ) ;
expect ( result . message ) . toMatch ( jasmine . pp ( expected ) ) ;
expect ( result . actual ) . toEqual ( actual ) ;
2009-10-30 03:33:01 +00:00
expect ( result . expected ) . toEqual ( expected ) ;
2009-10-30 00:03:24 +00:00
} ) ;
it ( "toNotContain to build an ExpectationResult" , function ( ) {
var actual = [ 'a' , 'b' , 'c' ] ;
var matcher = match ( actual ) ;
var expected = 'b' ;
matcher . toNotContain ( expected ) ;
2009-12-24 22:01:13 +00:00
var result = lastResult ( ) ;
2009-10-30 00:03:24 +00:00
expect ( result . matcherName ) . toEqual ( "toNotContain" ) ;
2010-08-04 20:52:38 +00:00
expect ( result . passed ( ) ) . toFail ( ) ;
2009-10-30 00:03:24 +00:00
expect ( result . message ) . toMatch ( jasmine . pp ( actual ) ) ;
expect ( result . message ) . toMatch ( 'not contain' ) ;
expect ( result . message ) . toMatch ( jasmine . pp ( expected ) ) ;
expect ( result . actual ) . toEqual ( actual ) ;
2009-10-30 03:33:01 +00:00
expect ( result . expected ) . toEqual ( expected ) ;
2009-05-29 03:02:15 +00:00
} ) ;
2009-08-14 22:39:28 +00:00
it ( "toBeLessThan should pass if actual is less than expected" , function ( ) {
2010-08-04 20:52:38 +00:00
expect ( match ( 37 ) . toBeLessThan ( 42 ) ) . toPass ( ) ;
expect ( match ( 37 ) . toBeLessThan ( - 42 ) ) . toFail ( ) ;
expect ( match ( 37 ) . toBeLessThan ( 37 ) ) . toFail ( ) ;
2009-08-14 22:39:28 +00:00
} ) ;
2009-10-30 00:03:24 +00:00
it ( "toBeLessThan to build an ExpectationResult" , function ( ) {
var actual = 3 ;
var matcher = match ( actual ) ;
var expected = 1 ;
matcher . toBeLessThan ( expected ) ;
2009-12-24 22:01:13 +00:00
var result = lastResult ( ) ;
2009-10-30 00:03:24 +00:00
expect ( result . matcherName ) . toEqual ( "toBeLessThan" ) ;
2010-08-04 20:52:38 +00:00
expect ( result . passed ( ) ) . toFail ( ) ;
2009-10-30 00:03:24 +00:00
expect ( result . message ) . toMatch ( jasmine . pp ( actual ) + ' to be less than' ) ;
expect ( result . message ) . toMatch ( jasmine . pp ( expected ) ) ;
expect ( result . actual ) . toEqual ( actual ) ;
2009-10-30 03:33:01 +00:00
expect ( result . expected ) . toEqual ( expected ) ;
2009-10-30 00:03:24 +00:00
} ) ;
2009-08-14 22:39:28 +00:00
it ( "toBeGreaterThan should pass if actual is greater than expected" , function ( ) {
2010-08-04 20:52:38 +00:00
expect ( match ( 37 ) . toBeGreaterThan ( 42 ) ) . toFail ( ) ;
expect ( match ( 37 ) . toBeGreaterThan ( - 42 ) ) . toPass ( ) ;
expect ( match ( 37 ) . toBeGreaterThan ( 37 ) ) . toFail ( ) ;
2009-08-14 22:39:28 +00:00
} ) ;
2009-10-30 00:03:24 +00:00
it ( "toBeGreaterThan to build an ExpectationResult" , function ( ) {
var actual = 1 ;
var matcher = match ( actual ) ;
var expected = 3 ;
matcher . toBeGreaterThan ( expected ) ;
2009-12-24 22:01:13 +00:00
var result = lastResult ( ) ;
2009-10-30 00:03:24 +00:00
expect ( result . matcherName ) . toEqual ( "toBeGreaterThan" ) ;
2010-08-04 20:52:38 +00:00
expect ( result . passed ( ) ) . toFail ( ) ;
2009-10-30 00:03:24 +00:00
expect ( result . message ) . toMatch ( jasmine . pp ( actual ) + ' to be greater than' ) ;
expect ( result . message ) . toMatch ( jasmine . pp ( expected ) ) ;
expect ( result . actual ) . toEqual ( actual ) ;
2009-10-30 03:33:01 +00:00
expect ( result . expected ) . toEqual ( expected ) ;
2009-10-30 00:03:24 +00:00
} ) ;
2010-08-04 01:51:31 +00:00
describe ( "toThrow" , function ( ) {
describe ( "when code block throws an exception" , function ( ) {
var throwingFn ;
2010-08-03 01:36:26 +00:00
2010-08-04 01:51:31 +00:00
beforeEach ( function ( ) {
throwingFn = function ( ) {
throw new Error ( "Fake Error" ) ;
} ;
} ) ;
2010-08-03 01:36:26 +00:00
2010-08-04 01:51:31 +00:00
it ( "should match any exception" , function ( ) {
2010-08-04 20:52:38 +00:00
expect ( match ( throwingFn ) . toThrow ( ) ) . toPass ( ) ;
2010-08-04 01:51:31 +00:00
} ) ;
2009-05-29 03:02:15 +00:00
2010-08-04 01:51:31 +00:00
it ( "should match exceptions specified by message" , function ( ) {
2010-08-04 20:52:38 +00:00
expect ( match ( throwingFn ) . toThrow ( "Fake Error" ) ) . toPass ( ) ;
expect ( match ( throwingFn ) . toThrow ( "Other Error" ) ) . toFail ( ) ;
2010-08-04 01:51:31 +00:00
expect ( lastResult ( ) . message ) . toMatch ( "Other Error" ) ;
} ) ;
2009-05-29 03:02:15 +00:00
2010-08-04 01:51:31 +00:00
it ( "should match exceptions specified by Error" , function ( ) {
2010-08-04 20:52:38 +00:00
expect ( match ( throwingFn ) . toThrow ( new Error ( "Fake Error" ) ) ) . toPass ( ) ;
expect ( match ( throwingFn ) . toThrow ( new Error ( "Other Error" ) ) ) . toFail ( ) ;
2010-08-04 01:51:31 +00:00
expect ( lastResult ( ) . message ) . toMatch ( "Other Error" ) ;
} ) ;
2009-05-29 03:02:15 +00:00
2010-08-04 01:51:31 +00:00
describe ( "and matcher is inverted with .not" , function ( ) {
it ( "should match any exception" , function ( ) {
2010-08-04 20:52:38 +00:00
expect ( match ( throwingFn ) . not . toThrow ( ) ) . toFail ( ) ;
2010-08-26 01:16:37 +00:00
expect ( lastResult ( ) . message ) . toMatch ( /Expected function not to throw an exception/ ) ;
2010-08-04 01:51:31 +00:00
} ) ;
it ( "should match exceptions specified by message" , function ( ) {
2010-08-04 20:52:38 +00:00
expect ( match ( throwingFn ) . not . toThrow ( "Fake Error" ) ) . toFail ( ) ;
2010-08-04 01:51:31 +00:00
// expect(lastResult().message).toMatch(/Expected function not to throw Fake Error./);
2010-08-04 20:52:38 +00:00
expect ( match ( throwingFn ) . not . toThrow ( "Other Error" ) ) . toPass ( ) ;
2010-08-04 01:51:31 +00:00
} ) ;
it ( "should match exceptions specified by Error" , function ( ) {
2010-08-04 20:52:38 +00:00
expect ( match ( throwingFn ) . not . toThrow ( new Error ( "Fake Error" ) ) ) . toFail ( ) ;
2010-08-04 01:51:31 +00:00
// expect(lastResult().message).toMatch("Other Error");
2010-08-04 20:52:38 +00:00
expect ( match ( throwingFn ) . not . toThrow ( new Error ( "Other Error" ) ) ) . toPass ( ) ;
2010-08-04 01:51:31 +00:00
} ) ;
} ) ;
} ) ;
2009-11-12 17:47:44 +00:00
2010-08-04 01:51:31 +00:00
describe ( "when actual is not a function" , function ( ) {
it ( "should fail with an exception" , function ( ) {
var exception = catchException ( function ( ) {
match ( 'not-a-function' ) . toThrow ( ) ;
} ) ;
expect ( exception ) . toBeDefined ( ) ;
expect ( exception . message ) . toEqual ( 'Actual is not a function' ) ;
} ) ;
2009-05-29 03:02:15 +00:00
2010-08-04 01:51:31 +00:00
describe ( "and matcher is inverted with .not" , function ( ) {
it ( "should fail with an exception" , function ( ) {
var exception = catchException ( function ( ) {
match ( 'not-a-function' ) . not . toThrow ( ) ;
} ) ;
expect ( exception ) . toBeDefined ( ) ;
expect ( exception . message ) . toEqual ( 'Actual is not a function' ) ;
} ) ;
} ) ;
} ) ;
2009-05-29 03:02:15 +00:00
2010-08-04 01:51:31 +00:00
2010-08-26 01:16:37 +00:00
describe ( "when code block does not throw an exception" , function ( ) {
2010-08-04 01:51:31 +00:00
it ( "should fail (or pass when inverted with .not)" , function ( ) {
expect ( match ( function ( ) {
2010-08-04 20:52:38 +00:00
} ) . toThrow ( ) ) . toFail ( ) ;
2010-08-04 01:51:31 +00:00
expect ( lastResult ( ) . message ) . toEqual ( 'Expected function to throw an exception.' ) ;
} ) ;
} ) ;
2009-12-24 22:01:13 +00:00
} ) ;
describe ( ".not.matcher" , function ( ) {
it ( "should invert the sense of any matcher" , function ( ) {
2010-08-04 20:52:38 +00:00
expect ( match ( 37 ) . not . toBeGreaterThan ( 42 ) ) . toPass ( ) ;
expect ( match ( 42 ) . not . toBeGreaterThan ( 37 ) ) . toFail ( ) ;
expect ( match ( "abc" ) . not . toEqual ( "def" ) ) . toPass ( ) ;
expect ( match ( "abc" ) . not . toEqual ( "abc" ) ) . toFail ( ) ;
2009-12-24 22:01:13 +00:00
} ) ;
it ( "should provide an inverted default message" , function ( ) {
match ( 37 ) . not . toBeGreaterThan ( 42 ) ;
expect ( lastResult ( ) . message ) . toEqual ( "Passed." ) ;
match ( 42 ) . not . toBeGreaterThan ( 37 ) ;
expect ( lastResult ( ) . message ) . toEqual ( "Expected 42 not to be greater than 37." ) ;
} ) ;
2009-05-29 03:02:15 +00:00
2009-12-24 22:01:13 +00:00
it ( "should use the second message when the matcher sets an array of custom messages" , function ( ) {
spec . addMatchers ( {
custom : function ( ) {
this . message = function ( ) {
return [ 'Expected it was called.' , 'Expected it wasn\'t called.' ] ;
} ;
return this . actual ;
}
} ) ;
2009-05-29 03:02:15 +00:00
2009-12-24 22:01:13 +00:00
match ( true ) . custom ( ) ;
expect ( lastResult ( ) . message ) . toEqual ( "Passed." ) ;
match ( false ) . custom ( ) ;
expect ( lastResult ( ) . message ) . toEqual ( "Expected it was called." ) ;
match ( true ) . not . custom ( ) ;
expect ( lastResult ( ) . message ) . toEqual ( "Expected it wasn't called." ) ;
match ( false ) . not . custom ( ) ;
expect ( lastResult ( ) . message ) . toEqual ( "Passed." ) ;
} ) ;
2009-05-29 03:02:15 +00:00
} ) ;
2009-11-13 17:40:04 +00:00
describe ( "spy matchers >>" , function ( ) {
2009-10-30 00:03:24 +00:00
var TestClass ;
beforeEach ( function ( ) {
2009-11-13 17:21:34 +00:00
TestClass = {
normalFunction : function ( ) {
} ,
spyFunction : jasmine . createSpy ( "My spy" )
} ;
2009-10-30 00:03:24 +00:00
} ) ;
2009-11-12 17:47:44 +00:00
2009-11-13 17:40:04 +00:00
function shouldThrowAnExceptionWhenInvokedOnANonSpy ( methodName ) {
return function ( ) {
expect ( function ( ) {
match ( TestClass . normalFunction ) [ methodName ] ( ) ;
} ) . toThrow ( 'Expected a spy, but got Function.' ) ;
expect ( function ( ) {
2009-11-26 16:12:06 +00:00
match ( jasmine . undefined ) [ methodName ] ( ) ;
2009-11-13 17:40:04 +00:00
} ) . toThrow ( 'Expected a spy, but got undefined.' ) ;
expect ( function ( ) {
match ( { some : 'object' } ) [ methodName ] ( ) ;
} ) . toThrow ( 'Expected a spy, but got { some : \'object\' }.' ) ;
2010-03-06 03:34:37 +00:00
expect ( function ( ) {
match ( "<b>" ) [ methodName ] ( ) ;
} ) . toThrow ( 'Expected a spy, but got \'<b>\'.' ) ;
2009-11-13 17:40:04 +00:00
} ;
}
2010-06-24 17:34:03 +00:00
describe ( "toHaveBeenCalled" , function ( ) {
it ( "should pass if the spy was called" , function ( ) {
2010-08-04 20:52:38 +00:00
expect ( match ( TestClass . spyFunction ) . toHaveBeenCalled ( ) ) . toFail ( ) ;
2009-11-12 17:47:44 +00:00
2009-11-13 17:30:22 +00:00
TestClass . spyFunction ( ) ;
2010-08-04 20:52:38 +00:00
expect ( match ( TestClass . spyFunction ) . toHaveBeenCalled ( ) ) . toPass ( ) ;
2009-11-13 17:30:22 +00:00
} ) ;
2009-10-30 03:33:01 +00:00
2009-11-13 17:30:22 +00:00
it ( "should throw an exception when invoked with any arguments" , function ( ) {
2009-11-12 17:47:44 +00:00
expect ( function ( ) {
2010-06-24 17:34:03 +00:00
match ( TestClass . normalFunction ) . toHaveBeenCalled ( "unwanted argument" ) ;
} ) . toThrow ( 'toHaveBeenCalled does not take arguments, use toHaveBeenCalledWith' ) ;
2009-11-13 17:30:22 +00:00
} ) ;
2009-11-12 17:47:44 +00:00
2010-06-24 17:34:03 +00:00
it ( 'should throw an exception when invoked on a non-spy' , shouldThrowAnExceptionWhenInvokedOnANonSpy ( 'toHaveBeenCalled' ) ) ;
2009-10-30 00:03:24 +00:00
} ) ;
2009-05-29 03:02:15 +00:00
2010-06-24 17:34:03 +00:00
describe ( "wasCalled" , function ( ) {
it ( "should alias toHaveBeenCalled" , function ( ) {
spyOn ( TestClass , 'normalFunction' ) ;
TestClass . normalFunction ( ) ;
expect ( TestClass . normalFunction ) . wasCalled ( ) ;
} ) ;
} ) ;
2009-11-13 17:30:22 +00:00
describe ( "wasNotCalled" , function ( ) {
it ( "should pass iff the spy was not called" , function ( ) {
2010-08-04 20:52:38 +00:00
expect ( match ( TestClass . spyFunction ) . wasNotCalled ( ) ) . toPass ( ) ;
2009-11-13 17:21:34 +00:00
2009-11-13 17:30:22 +00:00
TestClass . spyFunction ( ) ;
2010-08-04 20:52:38 +00:00
expect ( match ( TestClass . spyFunction ) . wasNotCalled ( ) ) . toFail ( ) ;
2009-11-13 17:30:22 +00:00
} ) ;
it ( "should throw an exception when invoked with any arguments" , function ( ) {
expect ( function ( ) {
match ( TestClass . normalFunction ) . wasNotCalled ( "unwanted argument" ) ;
} ) . toThrow ( 'wasNotCalled does not take arguments' ) ;
} ) ;
2009-11-13 17:40:04 +00:00
it ( 'should throw an exception when invoked on a non-spy' , shouldThrowAnExceptionWhenInvokedOnANonSpy ( 'wasNotCalled' ) ) ;
2009-11-13 17:30:22 +00:00
} ) ;
2009-10-30 03:33:01 +00:00
2010-06-24 17:34:03 +00:00
describe ( "toHaveBeenCalledWith" , function ( ) {
it ( 'toHaveBeenCalledWith should return true if it was called with the expected args' , function ( ) {
2009-11-13 17:30:22 +00:00
TestClass . spyFunction ( 'a' , 'b' , 'c' ) ;
2010-08-04 20:52:38 +00:00
expect ( match ( TestClass . spyFunction ) . toHaveBeenCalledWith ( 'a' , 'b' , 'c' ) ) . toPass ( ) ;
2009-10-30 03:33:01 +00:00
} ) ;
2009-11-13 17:30:22 +00:00
it ( 'should return false if it was not called with the expected args' , function ( ) {
TestClass . spyFunction ( 'a' , 'b' , 'c' ) ;
var expected = match ( TestClass . spyFunction ) ;
2010-08-04 20:52:38 +00:00
expect ( expected . toHaveBeenCalledWith ( 'c' , 'b' , 'a' ) ) . toFail ( ) ;
2009-12-24 22:01:13 +00:00
var result = lastResult ( ) ;
2010-08-04 20:52:38 +00:00
expect ( result . passed ( ) ) . toFail ( ) ;
2009-11-13 17:30:22 +00:00
expect ( result . expected ) . toEqual ( [ 'c' , 'b' , 'a' ] ) ;
expect ( result . actual . mostRecentCall . args ) . toEqual ( [ 'a' , 'b' , 'c' ] ) ;
2010-02-25 03:27:43 +00:00
expect ( result . message ) . toContain ( jasmine . pp ( result . expected ) ) ;
expect ( result . message ) . toContain ( jasmine . pp ( result . actual . mostRecentCall . args ) ) ;
} ) ;
it ( 'should return false if it was not called' , function ( ) {
var expected = match ( TestClass . spyFunction ) ;
2010-08-04 20:52:38 +00:00
expect ( expected . toHaveBeenCalledWith ( 'c' , 'b' , 'a' ) ) . toFail ( ) ;
2010-02-27 17:00:35 +00:00
var result = lastResult ( ) ;
2010-08-04 20:52:38 +00:00
expect ( result . passed ( ) ) . toFail ( ) ;
2010-02-25 03:27:43 +00:00
expect ( result . expected ) . toEqual ( [ 'c' , 'b' , 'a' ] ) ;
expect ( result . actual . argsForCall ) . toEqual ( [ ] ) ;
expect ( result . message ) . toContain ( jasmine . pp ( result . expected ) ) ;
2009-11-13 17:30:22 +00:00
} ) ;
2009-11-13 17:21:34 +00:00
2009-11-13 17:30:22 +00:00
it ( 'should allow matches across multiple calls' , function ( ) {
TestClass . spyFunction ( 'a' , 'b' , 'c' ) ;
TestClass . spyFunction ( 'd' , 'e' , 'f' ) ;
2010-08-13 07:32:32 +00:00
var expected = match ( TestClass . spyFunction ) ;
2010-08-04 20:52:38 +00:00
expect ( expected . toHaveBeenCalledWith ( 'a' , 'b' , 'c' ) ) . toPass ( ) ;
expect ( expected . toHaveBeenCalledWith ( 'd' , 'e' , 'f' ) ) . toPass ( ) ;
expect ( expected . toHaveBeenCalledWith ( 'x' , 'y' , 'z' ) ) . toFail ( ) ;
2009-11-13 17:30:22 +00:00
} ) ;
2009-10-30 03:33:01 +00:00
2010-08-13 07:32:32 +00:00
it ( "should return a decent message" , function ( ) {
TestClass . spyFunction ( 'a' , 'b' , 'c' ) ;
TestClass . spyFunction ( 'd' , 'e' , 'f' ) ;
var expected = match ( TestClass . spyFunction ) ;
expect ( expected . toHaveBeenCalledWith ( 'a' , 'b' ) ) . toFail ( ) ;
expect ( lastResult ( ) . message ) . toEqual ( "Expected spy to have been called with [ 'a', 'b' ] but was called with [ [ 'a', 'b', 'c' ], [ 'd', 'e', 'f' ] ]" ) ;
} ) ;
it ( "should return a decent message when inverted" , function ( ) {
TestClass . spyFunction ( 'a' , 'b' , 'c' ) ;
TestClass . spyFunction ( 'd' , 'e' , 'f' ) ;
var expected = match ( TestClass . spyFunction ) ;
expect ( expected . not . toHaveBeenCalledWith ( 'a' , 'b' , 'c' ) ) . toFail ( ) ;
expect ( lastResult ( ) . message ) . toEqual ( "Expected spy not to have been called with [ 'a', 'b', 'c' ] but was called with [ [ 'a', 'b', 'c' ], [ 'd', 'e', 'f' ] ]" ) ;
} ) ;
2010-06-24 17:34:03 +00:00
it ( 'should throw an exception when invoked on a non-spy' , shouldThrowAnExceptionWhenInvokedOnANonSpy ( 'toHaveBeenCalledWith' ) ) ;
2009-10-30 03:33:01 +00:00
2009-11-13 17:30:22 +00:00
describe ( "to build an ExpectationResult" , function ( ) {
beforeEach ( function ( ) {
var currentSuite ;
var spec ;
currentSuite = env . describe ( 'default current suite' , function ( ) {
spec = env . it ( ) ;
} , spec ) ;
TestClass = { someFunction : function ( a , b ) {
} } ;
spec . spyOn ( TestClass , 'someFunction' ) ;
2009-11-12 20:03:55 +00:00
} ) ;
2009-11-13 17:30:22 +00:00
it ( "should should handle the case of a spy" , function ( ) {
TestClass . someFunction ( 'a' , 'c' ) ;
var matcher = match ( TestClass . someFunction ) ;
2010-06-24 17:34:03 +00:00
matcher . toHaveBeenCalledWith ( 'a' , 'b' ) ;
2009-11-13 17:30:22 +00:00
2009-12-24 22:01:13 +00:00
var result = lastResult ( ) ;
2010-06-24 17:34:03 +00:00
expect ( result . matcherName ) . toEqual ( "toHaveBeenCalledWith" ) ;
2010-08-04 20:52:38 +00:00
expect ( result . passed ( ) ) . toFail ( ) ;
2010-02-25 03:27:43 +00:00
expect ( result . message ) . toContain ( jasmine . pp ( [ 'a' , 'b' ] ) ) ;
expect ( result . message ) . toContain ( jasmine . pp ( [ 'a' , 'c' ] ) ) ;
2009-11-13 17:30:22 +00:00
expect ( result . actual ) . toEqual ( TestClass . someFunction ) ;
expect ( result . expected ) . toEqual ( [ 'a' , 'b' ] ) ;
2009-11-12 20:03:55 +00:00
} ) ;
2009-10-30 03:33:01 +00:00
} ) ;
2009-10-30 00:03:24 +00:00
} ) ;
2010-02-25 03:27:43 +00:00
2010-06-24 17:34:03 +00:00
describe ( "wasCalledWith" , function ( ) {
it ( "should alias toHaveBeenCalledWith" , function ( ) {
spyOn ( TestClass , 'normalFunction' ) ;
TestClass . normalFunction ( 123 ) ;
expect ( TestClass . normalFunction ) . wasCalledWith ( 123 ) ;
} ) ;
} ) ;
2010-02-25 03:27:43 +00:00
describe ( "wasNotCalledWith" , function ( ) {
it ( 'should return true if the spy was NOT called with the expected args' , function ( ) {
TestClass . spyFunction ( 'a' , 'b' , 'c' ) ;
2010-08-04 20:52:38 +00:00
expect ( match ( TestClass . spyFunction ) . wasNotCalledWith ( 'c' , 'b' , 'a' ) ) . toPass ( ) ;
2010-02-25 03:27:43 +00:00
} ) ;
it ( 'should return false if it WAS called with the expected args' , function ( ) {
TestClass . spyFunction ( 'a' , 'b' , 'c' ) ;
var expected = match ( TestClass . spyFunction ) ;
2010-08-04 20:52:38 +00:00
expect ( expected . wasNotCalledWith ( 'a' , 'b' , 'c' ) ) . toFail ( ) ;
2010-02-27 17:00:35 +00:00
var result = lastResult ( ) ;
2010-08-04 20:52:38 +00:00
expect ( result . passed ( ) ) . toFail ( ) ;
2010-02-25 03:27:43 +00:00
expect ( result . expected ) . toEqual ( [ 'a' , 'b' , 'c' ] ) ;
expect ( result . actual . mostRecentCall . args ) . toEqual ( [ 'a' , 'b' , 'c' ] ) ;
expect ( result . message ) . toContain ( jasmine . pp ( result . expected ) ) ;
} ) ;
it ( 'should return true if it was not called' , function ( ) {
var expected = match ( TestClass . spyFunction ) ;
2010-08-04 20:52:38 +00:00
expect ( expected . wasNotCalledWith ( 'c' , 'b' , 'a' ) ) . toPass ( ) ;
2010-06-24 17:34:03 +00:00
} ) ;
2010-02-25 03:27:43 +00:00
it ( 'should allow matches across multiple calls' , function ( ) {
var expected = match ( TestClass . spyFunction ) ;
TestClass . spyFunction ( 'a' , 'b' , 'c' ) ;
TestClass . spyFunction ( 'd' , 'e' , 'f' ) ;
2010-08-04 20:52:38 +00:00
expect ( expected . wasNotCalledWith ( 'a' , 'b' , 'c' ) ) . toFail ( ) ;
expect ( expected . wasNotCalledWith ( 'd' , 'e' , 'f' ) ) . toFail ( ) ;
expect ( expected . wasNotCalledWith ( 'x' , 'y' , 'z' ) ) . toPass ( ) ;
2010-02-25 03:27:43 +00:00
} ) ;
it ( 'should throw an exception when invoked on a non-spy' , shouldThrowAnExceptionWhenInvokedOnANonSpy ( 'wasNotCalledWith' ) ) ;
} ) ;
2009-10-30 00:03:24 +00:00
} ) ;
2010-08-04 20:52:38 +00:00
describe ( "all matchers" , function ( ) {
2010-08-26 01:16:37 +00:00
it ( "should return null, for future-proofing, since we might eventually allow matcher chaining" , function ( ) {
2010-08-04 20:52:38 +00:00
expect ( match ( true ) . toBe ( true ) ) . toBeUndefined ( ) ;
2010-08-06 06:10:09 +00:00
} ) ;
} ) ;
2009-11-12 17:47:44 +00:00
} ) ;