Questions tagged [matcher]

Matchers are objects used among other things by testing libraries to check, if an object matches an abstract description of an expected state. Do not use this tag for use of the Matcher class for regular expression matching, use [regex] instead.

The most basic matcher is a function from an object to boolean, where the return value denotes whether the argument matches the description represented by the matcher. Apart from doing the actual matching matchers most of the time also provide some information about why a matcher did not match, and they can be combined using methods or functions, that take matchers as an argument and return new matchers (see the example below).

Matchers allow expectations to be written in an internal DSL. For example using Hamcrest and JUnit assertions can be written like this:

assertThat("nuts", biscuit.nutCount(), allOf(greaterThan(3), evenNumber()));

This is relatively easy to understand, even without knowing how it exactly works, and it will provide failure messages that explain the reason why the assertion failed. In the above example allOf(), greaterThan() and evenNumber() are all methods returning matchers.

Matchers are also used in other libraries, for example mock libraries use matchers in order to formulate expectations about calls to mocks, stubs or spies, like in the following example with Mockito.

when(mockedList.get(anyInt())).thenReturn("element");

Here anyInt() returns a matcher that matches any Integer or int.

458 questions
22
votes
3 answers

Jest Equality Matcher For Strings That Disregards Whitespace

Jest's toEqual matcher takes whitespace into account when checking for equality. When formatting the expected value in tests it is impossible to do so in a way that matches a string containing newlines, tabs etc. Does Jest offer a way to disregard…
Undistraction
  • 42,754
  • 56
  • 195
  • 331
21
votes
6 answers

Jasmine toEqual for complex objects (mixed with functions)

Currently, I have a function that sometimes return an object with some functions inside. When using expect(...).toEqual({...}) it doesn't seem to match those complex objects. Objects having functions or the File class (from input type file), it just…
pocesar
  • 6,860
  • 6
  • 56
  • 88
20
votes
7 answers

Rspec view testing with capybara and rails3

I really like the way RSpec is able to separate controller and view tests but have some problems with getting capybara matchers to work in a view test. What i basically try to achieve is sth like this: describe "some page" do it "should render…
dahpgjgamgan
  • 2,977
  • 4
  • 25
  • 26
20
votes
3 answers

Is there a Hamcrest matcher to check that a Collection is neither empty nor null?

Is there a Hamcrest matcher which checks that the argument is neither an empty Collection nor null? I guess I could always use both(notNullValue()).and(not(hasSize(0)) but I was wondering whether there is a simpler way and I missed it.
jhyot
  • 3,733
  • 1
  • 27
  • 44
18
votes
2 answers

Replace HTML codes with equivalent characters in Java

Currently I'm working on converting HTML codes with equivalent characters in java. I need to convert the below code to characters. è - è ® - ® & - & ñ - ñ & - & I tried using the regex pattern…
Raja Asthana
  • 2,080
  • 2
  • 19
  • 35
17
votes
3 answers

Does an RSpec2 matcher for matching Hashes exist?

Note to future readers: think RSpec does not consider your Hashes equal? One might be an OrderedHash, but from the regular RSpec output you can't tell. This was the problem that prompted this post. Original question: Suppose I have a spec where I…
Confusion
  • 16,256
  • 8
  • 46
  • 71
17
votes
6 answers

During suite tests EasyMock says 0 matchers expected 1 recorded

So I've been using EasyMock's class extension for a while now. All of a sudden I'm getting this exception, but only when I run the entire test suite: java.lang.IllegalStateException: 0 matchers expected, 1 recorded. at…
holmes
  • 578
  • 1
  • 7
  • 16
16
votes
4 answers

How to do `cy.notContains(text)` in cypress?

I can check if text exists in cypress with cy.contains('hello'), but now I delete hello from the page, I want to check hello doesn't exist, how do I do something like cy.notContains('hello')?
Alien
  • 944
  • 2
  • 8
  • 22
16
votes
1 answer

rspec The expect syntax does not support operator matchers, so you must pass a matcher to `#to`

using the new expect syntax: expect(@line.filter_results_and_display_them).to == @processed Getting this error: ArgumentError: The expect syntax does not support operator matchers, so you must pass a matcher to '#to'
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
15
votes
2 answers

Mockito Matchers: matching a Class type in parameter list

I am working with Java, Spring's RestTemplate, and Mockito, using Eclipse. I am trying to mock Spring's rest template, and the last parameter for the method I am mocking is a Class type. Below is the signature for the function: public
piper1970
  • 518
  • 1
  • 3
  • 14
15
votes
2 answers

Mockito: Match any String except one

How can I write a matcher using Mockito that matches any string except a specific one? I have tried using some hamcrest matchers to negate and combine other matchers, but the hamcrest matchers all return values of type Matcher which dont work…
Stephan
  • 16,509
  • 7
  • 35
  • 61
15
votes
1 answer

Mockito Matchers.any(...) on one argument only

I want to do this: verify(function, Mockito.times(1)).doSomething(argument1, Matchers.any(Argument2.class)); Where argument1 is a specfic instance of type Argument1 and argument2 is any instance of the type Argument2. But I get an…
Kevvvvyp
  • 1,704
  • 2
  • 18
  • 38
15
votes
5 answers

Junit Matcher for comparators?

For several days I am using now Junit's Matchers feature. Everything is working OK but I am looking for a matcher which uses a comparator for comparing and which does not rely on the objects equals methodes. I want to replace Assert.assertThat(one,…
14
votes
2 answers

Is It Possible To Extend A Jest / Expect Matcher

I would like to extend Jest's isEqual matcher so that the expected value is transformed before comparison (this allows me to use multiline strings in tests). All I need to do is run the expected value through a the indentToFirstLine function from…
Undistraction
  • 42,754
  • 56
  • 195
  • 331
14
votes
2 answers

How to stub a method call with an implicit matcher in Mockito and Scala

My application code uses AService trait AService { def registerNewUser (username: String)(implicit tenant: Tenant): Future[Response] } to register a new user. Class Tenant is a simple case class: case class Tenant(val vstNumber:String, val…
simou
  • 2,467
  • 4
  • 30
  • 39
1
2
3
30 31