diff --git a/v3.0/configuration/index.html b/v3.0/configuration/index.html index 90cb832c..5485ea59 100644 --- a/v3.0/configuration/index.html +++ b/v3.0/configuration/index.html @@ -1152,7 +1152,7 @@

Exampletemplate parameter. +
  • The style of mock to be generated is specified using the template parameter.
  • An output file may contain multiple mocks, but the only rule is that all the mocks in the file must come from the same package. Because of this, mocks for different packages must go in different files.

    Parameter Descriptions

    @@ -1245,7 +1245,7 @@

    Parameter DescriptionsUse outpkg to specify the package name of the generated mocks. -packages +packages null A dictionary containing configuration describing the packages and interfaces to generate mocks for. @@ -1257,7 +1257,7 @@

    Parameter Descriptions -recursive +recursive false When set to true on a particular package, mockery will recursively search for all sub-packages and inject those packages into the config map. diff --git a/v3.0/faq/index.html b/v3.0/faq/index.html index b13e844e..a318e32a 100644 --- a/v3.0/faq/index.html +++ b/v3.0/faq/index.html @@ -842,32 +842,6 @@ - - -
  • - - - - - Multiple Expectations With Identical Arguments - - - - - -
  • - -
  • - - - - - Variadic Arguments - - - - -
  • @@ -1002,32 +976,6 @@ -
  • - -
  • - - - - - Multiple Expectations With Identical Arguments - - - - - -
  • - -
  • - - - - - Variadic Arguments - - - - -
  • @@ -1116,73 +1064,14 @@

    internal error: packa
  • Reinstall mockery
  • Additionally, this issue only happens when compiling mockery from source, such as with go install. Our docs recommend not to use go install as the success of your build depends on the compatibility of your Go version with the semantics in use. You would not encounter this issue if using one of the installation methods that install pre-built binaries, like downloading the .tar.gz binaries, or through brew install.

    -

    Multiple Expectations With Identical Arguments

    -

    There might be instances where you want a mock to return different values on successive calls that provide the same arguments. For example, we might want to test this behavior:

    -
    Go
    // Return "foo" on the first call
    -getter := NewGetter()
    -assert(t, "foo", getter.Get("key"))
    -
    -// Return "bar" on the second call
    -assert(t, "bar", getter.Get("key"))
    -
    -

    This can be done by using the .Once() method on the mock call expectation:

    -
    Go
    mockGetter := NewMockGetter(t)
    -mockGetter.EXPECT().Get(mock.anything).Return("foo").Once()
    -mockGetter.EXPECT().Get(mock.anything).Return("bar").Once()
    -
    -

    Or you can identify an arbitrary number of times each value should be returned:

    -
    Go
    mockGetter := NewMockGetter(t)
    -mockGetter.EXPECT().Get(mock.anything).Return("foo").Times(4)
    -mockGetter.EXPECT().Get(mock.anything).Return("bar").Times(2)
    -
    -

    Note that with proper Go support in your IDE, all the available methods are self-documented in autocompletion help contexts.

    -

    Variadic Arguments

    -

    Consider if we have a function func Bar(message ...string) error. A typical assertion might look like this:

    -
    Go
    func TestFoo(t *testing.T) {
    -  m := NewMockFoo(t)
    -  m.On("Bar", "hello", "world").Return(nil)
    -
    -

    We might also want to make an assertion that says "any number of variadic arguments":

    -
    Go
    m.On("Bar", mock.Anything).Return(nil)
    -
    -

    However, what we've given to mockery is ambiguous because it is impossible to distinguish between these two intentions:

    -
      -
    1. Any number of variadic arguments of any value
    2. -
    3. A single variadic argument of any value
    4. -
    -

    This is fixed in #359 where you can provide unroll-variadic: False to get back to the old behavior. Thus, if you want to assert (1), you can then do:

    -
    Go
    m.On("Bar", mock.Anything).Return(nil)
    -
    -

    If you want to assert (2), you must set unroll-variadic: True. Then this assertion's intention will be modified to mean the second case:

    -
    Go
    m.On("Bar", mock.Anything).Return(nil)
    -
    -

    An upstream patch to testify is currently underway to allow passing mock.Anything directly to the variadic slice: https://github.com/stretchr/testify/pull/1348

    -

    If this is merged, it would become possible to describe the above two cases respectively:

    -
    Go
    // case 1
    -m.On("Bar", mock.Anything).Return(nil)
    -// case 2
    -m.On("Bar", []interface{}{mock.Anything}).Return(nil)
    -
    -

    References:

    -

    Semantic Versioning

    -

    The versioning in this project applies only to the behavior of the mockery binary itself. This project explicitly does not promise a stable internal API, but rather a stable executable. The versioning applies to the following:

    -
      -
    1. CLI arguments.
    2. -
    3. Parsing of Go code. New features in the Go language will be supported in a backwards-compatible manner, except during major version bumps.
    4. -
    5. Behavior of mock objects. Mock objects can be considered to be part of the public API.
    6. -
    7. Behavior of mockery given a set of arguments.
    8. -
    -

    What the version does not track:

    +

    The mockery project follows the standard Semantic Versioning Semantics. The versioning applies to the following areas:

      -
    1. The interfaces, objects, methods etc. in the vektra/mockery package.
    2. -
    3. Compatibility of go get-ing mockery with new or old versions of Go.
    4. +
    5. The shape of mocks generated by pre-curated templates.
    6. +
    7. Functions and data provided to templates specified with template: "file://".
    8. +
    9. Configuration options.
    +

    Mockery is not meant to be used as an imported library. Importing mockery code in external modules is not supported.

    Mocking interfaces in main

    When your interfaces are in the main package, you should supply the --inpackage flag. This will generate mocks in the same package as the target code, avoiding import issues.

    diff --git a/v3.0/search/search_index.json b/v3.0/search/search_index.json index d5134918..fd5e70d8 100644 --- a/v3.0/search/search_index.json +++ b/v3.0/search/search_index.json @@ -1 +1 @@ -{"config":{"lang":["en"],"separator":"[\\s\\-]+","pipeline":["stopWordFilter"],"fields":{"title":{"boost":1000.0},"text":{"boost":1.0},"tags":{"boost":1000000.0}}},"docs":[{"location":"","title":"mockery","text":"

    Alpha Test

    Mockery v3 is currently in alpha and should not be used for production.

    Mockery is a project that creates mock implementations of Golang interfaces. It inspects source code and generates implementations of the interface that aid in testing.

    In addition to providing a number of different styles of mocks, mockery also allows users to provide their own template files that will then be rendered using a set of template data, methods, and functions that provide comprehensive typing information about the Go interface in question.

    "},{"location":"#why-mockery","title":"Why mockery?","text":"

    When you have an interface like this:

    db.go
    type DB interface {\n    Get(val string) string\n}\n

    and a function that takes this interface:

    db_getter.go
    func getFromDB(db DB) string {\n    return db.Get(\"ice cream\")\n}\n

    We can use simple configuration to generate a mock implementation for the interface:

    .mockery.yaml
    packages:\n    github.com/org/repo:\n        interfaces:\n            DB:\n
    Bash
    $ mockery\n05 Mar 23 21:49 CST INF Starting mockery dry-run=false version=v3.0.0\n05 Mar 23 21:49 CST INF Using config: .mockery.yaml dry-run=false version=v3.0.0\n05 Mar 23 21:49 CST INF Generating mock dry-run=false interface=DB qualified-name=github.com/org/repo version=v3.0.0\n

    We can then use the mock object in a test:

    db_getter_test.go
    import (\n    \"testing\"\n\n    \"github.com/stretchr/testify/assert\"\n)\n\nfunc Test_getFromDB(t *testing.T) {\n    mockDB := NewMockDB(t)\n    mockDB.EXPECT().Get(\"ice cream\").Return(\"chocolate\").Once()\n    flavor := getFromDB(mockDB)\n    assert.Equal(t, \"chocolate\", flavor)\n}\n
    "},{"location":"#why-use-mockery","title":"Why use mockery?","text":"
    1. You gain access to a number of pre-curated mock implementations that can be used in testing. This includes traditional \"mockery-style\" mocks, as well as other styles from the open source community such as from https://github.com/matryer/moq. Such mocks allow you to quickly define how the implementation should behave under test without having to manually curate your own mocks/stubs/fakes.
    2. Mockery benefits from a large number of performance improvements that almost all other Go code-generation projects currently have not employed. This means that it's orders of magnitude faster for large codebases.
    3. Mockery provides a comprehensive, centralized, flexible, and simple configuration scheme driven off of yaml instead of relying on sprawling //go:generate commands.
    4. Mockery is a code-generation framework. While its original goal is to provide mock implementations for testing purposes, users can supply their own templates to auto-generate any kind of code that needs to be based off of interfaces.
    5. A number of high profile companies, projects, and communities trust Mockery.
    "},{"location":"#who-uses-mockery","title":"Who uses mockery?","text":"