File: generators.md

package info (click to toggle)
python-flexmock 0.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 460 kB
  • sloc: python: 3,802; makefile: 17; sh: 14
file content (24 lines) | stat: -rw-r--r-- 636 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Generators

In addition to returning values and raising exceptions, flexmock can also turn
the mocked method into a generator that yields successive values:

```python
>>> flexmock(plane).should_receive("flight_log").and_yield(
    "take off", "flight", "landing")
>>> for i in plane.flight_log():
>>>     print(i)
"take off"
"flight"
"landing"
```

You can also use Python's builtin `iter()` function to generate an iterable
return value:

```python
flexmock(plane, flight_log=iter(["take off", "flight", "landing"]))
```

In fact, the `and_yield()` modifier is just shorthand for
`should_receive().and_return(iter)` under the hood.