Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example usage with a mocking framework #15

Closed
realmhamdy opened this issue May 16, 2016 · 2 comments
Closed

Example usage with a mocking framework #15

realmhamdy opened this issue May 16, 2016 · 2 comments

Comments

@realmhamdy
Copy link

I'm trying to use Fruit with FakeIt in a unit test. Needless to say, I'm stuck.
I have quite a normal setup here for the code under test:

// component required by the main component
Component<RequiredComponent> getRequiredComponent() {
  return createComponent()
      .bind<RequiredComponent, RequiredComponentImpl>();
}

// component required in testing
class DependentComponent {
  INJECT(DependentComponent(RequiredComponent * reqComp, ASSISTED(double) arg)) = default;
}

using DependentFactory = function<unique_ptr<DependentComponent>(double)>;
Component<Required<RequiredComponent>, DependentFactory> getDependentComponent() {
  return createComponent().install(getRequiredComponent());
}

The compiler is happy and so I am. Now to the test:

Mock<RequiredComponent> mockRequired;
When(Method(mockRequired, someMethod)).Return(fakeData);
// now what ??

How to get the mock object, returned by mockRequired.get() and install it in the factory?

@poletti-marco
Copy link
Contributor

I don't understand why you're installing getRequiredComponent() in getDependentComponent().
As far as I understand, you don't want to use the real RequiredComponent object, you want to use a mock instead.
You could pass the mock object as a parameter to getDependentComponent() and do a bindInstance() there of the mock. E.g. something like:

Component<Required<RequiredComponent>, DependentFactory> 
    getDependentComponent(RequiredComponent& requiredComponent) {
  return createComponent().bindInstance(requiredComponent);
}
...
Mock<RequiredComponent> mockRequired;
When(Method(mockRequired, someMethod)).Return(fakeData);
Injector<DependentComponent> injector(getDependentComponent(mockRequired.get()));
...

@realmhamdy
Copy link
Author

realmhamdy commented May 20, 2016

Thank you @poletti-marco. That helped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants