question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I configure the auto mocker?

See original GitHub issue

I was previously using SpecsFor 5.0.1, in which I could do this:

public interface IBar : ISpecs
{
    IBaz Baz { get; set; }
}

public class Foo : Behavior<IBar>
{
    public override void SpecInit(IBar instance)
    {
        instance.MockContainer.Configure(cfg => cfg.For<IBaz>().Use(instance.Baz));
    }
}

After updating to SpecsFor 7.0.0, this is no longer available, and there is no documentation or summary of changes to consult regarding these breaking changes.

Where would I find the facility to configure the automocker’s behaviour?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
MattHoneycuttcommented, Feb 6, 2019

Ugh, I’ve really dropped the ball on supporting SpecsFor lately. If anyone still cares, here’s the approach I’m considering for resolving this issue.

I’m planning to add a new extension method, GetMockContainer(), to each implementation of SpecsFor (StructureMap and Autofac for now). Calling this on an ISpecs instance will return the underlying automocking container for the library you’re using.

Here’s an implementation of the extension method for StructureMap:

    public static class StructureMapAutoMockerExtensions
    {
        public static AutoMockedContainer GetMockContainer(this ISpecs specs)
        {
            dynamic specsDynamic = specs;

            return specsDynamic.Mocker.MoqAutoMocker.Container;
        }
    }

@bandoyer, if you’re still interested, you should be able to drop this in to your project, then update your spec like this:

public class FakeContextProvider : Behavior<INeedFakeContext>
{
	public override void SpecInit(INeedFakeContext instance)
	{
		instance.FakeContext = new FakeDbContext();

		// *** This is the issue ***
		// *** instance.Mocker is an instance of SomeService so the cast fails ***
		//var mocker = (StructureMapAutoMocker<INeedFakeContext>)instance.Mocker;
                var container = instance.GetMockContainer();
		container.Configure(cfg => 
		{ 
			cfg.For<IADAPContext>().Use(instance.FakeContext); 
		});
	}
}

Let me know if that approach works for your use case and makes sense. If so, I’ll get it added to the package.

1reaction
MattHoneycuttcommented, Oct 29, 2018

Hi @psyren89! I apologize for not having the docs updated yet.

In SpecsFor 7, you can do the following:

public class Foo : Behavior<IBar>
{
    public override void SpecInit(IBar instance)
    {
	    var mocker = (StructureMapAutoMocker<IBar>) instance.Mocker;

            mocker.MoqAutoMocker.Container.Configure(cfg =>
            {
                cfg.For<IBaz>().Use(instance.Baz)
            });
    }
}

I’m going to leave this issue open to remind me that we need to add an easier way of getting to this functionality.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Improving Unit Tests With AutoMocker
When AutoMocker creates the ViewModel it will use the specified dependencies and create loose mocks for all other dependencies. In this manner, you...
Read more >
moq/Moq.AutoMocker: An auto-mocking IoC container for ...
Simplest usage is to build an instance that you can unit test. var mocker = new AutoMocker(); var car = mocker. CreateInstance<Car>(); car....
Read more >
Auto mocking Explained
The automocker will keep track of all the mocks it creates, and allows you to retrieve them if you need to stub a...
Read more >
Sparky's Tool Tips: Moq.AutoMock
Verify mock behavior, you can get the auto-generated mock via AutoMocker's "GetMock<TMock>" method: autoMocker.GetMock<IFormatter>.Setup( ...
Read more >
Moq — Autofac 7.0.0 documentation
You can configure the automatic mocks and/or assert calls on them as you would normally with Moq. [Test] public void Test() { using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found