# Is there a way to force a wrong dispatch for test purposes?

**URL:** https://discourse.julialang.org/t/is-there-a-way-to-force-a-wrong-dispatch-for-test-purposes/121743
**Category:** New to Julia
**Created:** [October 25, 2024, 10:54am UTC](https://discourse.julialang.org/t/is-there-a-way-to-force-a-wrong-dispatch-for-test-purposes/121743 "2024-10-25T10:54:39Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)
#### Post date: [October 25, 2024, 10:54am UTC](https://discourse.julialang.org/t/is-there-a-way-to-force-a-wrong-dispatch-for-test-purposes/121743/1 "2024-10-25T10:54:39Z")

</div>

Hi,

Suppose I have two classes and a function as follows:

```julia
abstract struct A end
struct B<:A end

```

and then the function

```julia
f(x::A) = true
f(x::B) = true

```

The function is supposed to return the same thing in both cases, the method `f(x::B)` being only a performance improvement w.r.t. `f(x::A)`. To ensure that the functions are returning the same things, is there a way i could force, in my testing, the dispatch on `x = B()` to _forget_ about the second method and use the first one ?

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [October 25, 2024, 10:57am UTC](https://discourse.julialang.org/t/is-there-a-way-to-force-a-wrong-dispatch-for-test-purposes/121743/2 "2024-10-25T10:57:14Z")

</div>

I believe you’re looking for `invoke`.

```julia
  invoke(f, argtypes::Type, args...; kwargs...)

  Invoke a method for the given generic function f matching the specified types argtypes on the specified
  arguments args and passing the keyword arguments kwargs. The arguments args must conform with the
  specified types in argtypes, i.e. conversion is not automatically performed. This method allows invoking a
  method other than the most specific matching method, which is useful when the behavior of a more general
  definition is explicitly needed (often as part of the implementation of a more specific method of the same
  function).

```

---

<div class="post-metadata">

### Author: ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)
#### Post date: [October 25, 2024, 10:57am UTC](https://discourse.julialang.org/t/is-there-a-way-to-force-a-wrong-dispatch-for-test-purposes/121743/3 "2024-10-25T10:57:58Z")

</div>

Exactly what i wanted. Thanks ! So here’s the test I wanted:

```julia
x = B()
@test f(x) == invoke(f, Tuple{A}, x)

```

---

<div class="post-metadata">

### Author: ![danielwe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielwe/32/35657_2.png) [@danielwe](https://discourse.julialang.org/u/danielwe)
#### Post date: [October 25, 2024, 2:21pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-force-a-wrong-dispatch-for-test-purposes/121743/4 "2024-10-25T14:21:13Z")

</div>

There’s also a macro form that you may or may not prefer:

```julia
x = B()
@test f(x) == @invoke f(x::A)

```
