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

Moqqer.Create - With Params to Inject #34

Open
michal-ciechan opened this issue Mar 26, 2018 · 2 comments
Open

Moqqer.Create - With Params to Inject #34

michal-ciechan opened this issue Mar 26, 2018 · 2 comments

Comments

@michal-ciechan
Copy link
Owner

        [Test]
        public void Create_ConcreteClassInject_PassInInjectionParameter()
        {
            Assert.Inconclusive("TODO: Moqqer.Create - With Params to Inject Issue");

//            var ctorParam = new ClassWithoutParameterlessCtor(null); 
//            
//           var mock = _moq
//                .Create<ClassWithCtorContainingClassWithoutParameterlessCtor>(autogenerate: false, paramters: ctorParam);
//
//            mock.CtorParam.Should().Should().BeSameAs(ctorParam);
        }
@damianpumar
Copy link
Contributor

Hello @michal-ciechan in this issue do you mean when I have a class inject other class into constructor?
If this is true, I think create recursive Create() when the parameter type IsClass using reflection.

Cheers!

@michal-ciechan
Copy link
Owner Author

hey @damianpumar, it will already recursively generate concrete classes if you specify Create<T>(autogenerate: true).

This is so that you can provide a single instance to use for a specific Create<T>(). Similar to something like the below:

Example Class

class SystemUnderTest {
  // ctor
  public SystemUnderTest(StringBuilder builder, ISomeInterface interface) { ... }
}

Example Test

var sb = new StringBuilder("Some Test String");

_moq.Create<SystemUnderTest>(sb);

I would expect this to inject the local concrete instance variable sb into SystemUnderTest constructor parameter ctor(StringBuilder builder, ...).

There are a number of times I have needed to use a third party concrete type, which I want to pass in a single instace.

Currently I am using the following workaround:

Workaround

var sb = new StringBuilder("Some Test String");

_moq.Use(sb);

_moq.Create<SystemUnderTest>();

This uses my specific local variable sb as the ctor parameter, as it is registered with the container globally. The drawback for this workaround is that anywhere else a class requests StrignBuilder that specific instance will be passed in.

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