flexmock API

flexmock.flexmock(spec=None, **kwargs)[source]

Main entry point into the flexmock API.

This function is used to either generate a new fake object or take an existing object (or class or module) and use it as a basis for a partial mock. In case of a partial mock, the passed in object is modified to support basic Mock class functionality making it unnecessary to make successive flexmock() calls on the same objects to generate new expectations.

Examples:
>>> flexmock(SomeClass)
>>> SomeClass.should_receive('some_method')

NOTE: it’s safe to call flexmock() on the same object, it will detect when an object has already been partially mocked and return it each time.

Args:
  • spec: object (or class or module) to mock

  • kwargs: method/return_value pairs to attach to the object

Returns:

Mock object if no spec is provided. Otherwise return the spec object.

class flexmock.Mock(**kwargs)[source]

Fake object class returned by the flexmock() function.

new_instances(*kargs)[source]

Overrides __new__ method on the class to return custom objects.

Alias for should_receive(‘__new__’).and_return(kargs).one_by_one

Args:
  • kargs: objects to return on each successive call to __new__

Returns:
  • Expectation object

should_call(name)[source]

Creates a spy.

This means that the original method will be called rather than the fake version. However, we can still keep track of how many times it’s called and with what arguments, and apply expectations accordingly.

should_call is meaningless/not allowed for non-callable attributes.

Args:
  • name: string name of the method

Returns:
  • Expectation object

should_receive(name)[source]

Replaces the specified attribute with a fake.

Args:
  • name: string name of the attribute to replace

Returns:
  • Expectation object which can be used to modify the expectations on the fake attribute

class flexmock.Expectation(mock, name=None, return_value=None, original=None, method_type=None)[source]

Holds expectations about methods.

The information contained in the Expectation object includes method name, its argument list, return values, and any exceptions that the method might raise.

and_raise(exception, *kargs, **kwargs)[source]

Specifies the exception to be raised when this expectation is met.

Args:
  • exception: class or instance of the exception

  • kargs: optional keyword arguments to pass to the exception

  • kwargs: optional named arguments to pass to the exception

Returns:
  • self, i.e. can be chained with other Expectation methods

and_return(*values)[source]

Override the return value of this expectation’s method.

When and_return is given multiple times, each value provided is returned on successive invocations of the method. It is also possible to mix and_return with and_raise in the same manner to alternate between returning a value and raising and exception on different method invocations.

When combined with the one_by_one property, value is treated as a list of values to be returned in the order specified by successive calls to this method rather than a single list to be returned each time.

Args:
  • values: optional list of return values, defaults to None if not given

Returns:
  • self, i.e. can be chained with other Expectation methods

and_yield(*kargs)[source]

Specifies the list of items to be yielded on successive method calls.

In effect, the mocked object becomes a generator.

Returns:
  • self, i.e. can be chained with other Expectation methods

at_least()[source]

Modifies the associated times() expectation.

When given, an exception will only be raised if the method is called less than times() specified. Does nothing if times() is not given.

Returns:
  • self, i.e. can be chained with other Expectation methods

at_most()[source]

Modifies the associated “times” expectation.

When given, an exception will only be raised if the method is called more than times() specified. Does nothing if times() is not given.

Returns:
  • self, i.e. can be chained with other Expectation methods

mock()[source]

Return the mock associated with this expectation.

one_by_one()[source]

Modifies the return value to be treated as a list of return values.

Each value in the list is returned on successive invocations of the method.

Returns:
  • self, i.e. can be chained with other Expectation methods

ordered()[source]

Makes the expectation respect the order of should_receive statements.

An exception will be raised if methods are called out of order, determined by order of should_receive calls in the test.

Returns:
  • self, i.e. can be chained with other Expectation methods

replace_with(function)[source]

Gives a function to run instead of the mocked out one.

Args:
  • function: callable

Returns:
  • self, i.e. can be chained with other Expectation methods

times(number)[source]

Number of times this expectation’s method is expected to be called.

There are also 3 aliases for the times() method:

  • once() -> times(1)

  • twice() -> times(2)

  • never() -> times(0)

Args:
  • number: int

Returns:
  • self, i.e. can be chained with other Expectation methods

when(func)[source]

Sets an outside resource to be checked before executing the method.

Args:
  • func: function to call to check if the method should be executed

Returns:
  • self, i.e. can be chained with other Expectation methods

with_args(*kargs, **kwargs)[source]

Override the arguments used to match this expectation’s method.

Args:
  • kargs: optional keyword arguments

  • kwargs: optional named arguments

Returns:
  • self, i.e. can be chained with other Expectation methods