Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
.. index::
2
    single: Alternative shouldReceive Syntax
3
 
4
Alternative shouldReceive Syntax
5
================================
6
 
7
As of Mockery 1.0.0, we support calling methods as we would call any PHP method,
8
and not as string arguments to Mockery ``should*`` methods.
9
 
10
The two Mockery methods that enable this are ``allows()`` and ``expects()``.
11
 
12
Allows
13
------
14
 
15
We use ``allows()`` when we create stubs for methods that return a predefined
16
return value, but for these method stubs we don't care how many times, or if at
17
all, were they called.
18
 
19
.. code-block:: php
20
 
21
    $mock = \Mockery::mock('MyClass');
22
    $mock->allows([
23
        'name_of_method_1' => 'return value',
24
        'name_of_method_2' => 'return value',
25
    ]);
26
 
27
This is equivalent with the following ``shouldReceive`` syntax:
28
 
29
.. code-block:: php
30
 
31
    $mock = \Mockery::mock('MyClass');
32
    $mock->shouldReceive([
33
        'name_of_method_1' => 'return value',
34
        'name_of_method_2' => 'return value',
35
    ]);
36
 
37
Note that with this format, we also tell Mockery that we don't care about the
38
arguments to the stubbed methods.
39
 
40
If we do care about the arguments, we would do it like so:
41
 
42
.. code-block:: php
43
 
44
    $mock = \Mockery::mock('MyClass');
45
    $mock->allows()
46
        ->name_of_method_1($arg1)
47
        ->andReturn('return value');
48
 
49
This is equivalent with the following ``shouldReceive`` syntax:
50
 
51
.. code-block:: php
52
 
53
    $mock = \Mockery::mock('MyClass');
54
    $mock->shouldReceive('name_of_method_1')
55
        ->with($arg1)
56
        ->andReturn('return value');
57
 
58
Expects
59
-------
60
 
61
We use ``expects()`` when we want to verify that a particular method was called:
62
 
63
.. code-block:: php
64
 
65
    $mock = \Mockery::mock('MyClass');
66
    $mock->expects()
67
        ->name_of_method_1($arg1)
68
        ->andReturn('return value');
69
 
70
This is equivalent with the following ``shouldReceive`` syntax:
71
 
72
.. code-block:: php
73
 
74
    $mock = \Mockery::mock('MyClass');
75
    $mock->shouldReceive('name_of_method_1')
76
        ->once()
77
        ->with($arg1)
78
        ->andReturn('return value');
79
 
80
By default ``expects()`` sets up an expectation that the method should be called
81
once and once only. If we expect more than one call to the method, we can change
82
that expectation:
83
 
84
.. code-block:: php
85
 
86
    $mock = \Mockery::mock('MyClass');
87
    $mock->expects()
88
        ->name_of_method_1($arg1)
89
        ->twice()
90
        ->andReturn('return value');
91