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

Full example of using IObjectMother? #82

Closed
mcquiggd opened this issue Jan 17, 2021 · 2 comments
Closed

Full example of using IObjectMother? #82

mcquiggd opened this issue Jan 17, 2021 · 2 comments

Comments

@mcquiggd
Copy link

mcquiggd commented Jan 17, 2021

As per the title, I am a little confused as to how to use the ObjectMother feature of this library:

The snippets in the documentation are not quite in sync, referring to:

Given(() =>
            {
                SetThe<Customer>().To(EntityMother.CreateACustomer().With(x => x.Id = customerId));

EntityMother.CreateACustomer() is an apparently static method, perhaps a standard Builder pattern, but the EntityMother implementation is not described, so I am not sure. Other snippets refer to an apparently different approach, by implementing the IObjectMother interface:

public class CustomerStoreMother : IObjectMother
{
    public bool IsFallback { get; } = false;

    public bool Applies(Type type) => typeof(Foo).IsAssignableFrom(type);

    public object Create(Type type, IChillObjectResolver resolver)
    {
        return Substitute.For<ICustomerStore>();
    }
}

My understanding is that implementations of IObjectMother within the same assembly should be registered with the AutoFac container - but I am also not sure if this also needs to be referred to in some way in the Given method.

Any chance of a simple but complete sample? I have a feeling this project would be pretty popular, If there were samples that made the usage a little clearer... thanks!

@mcquiggd
Copy link
Author

mcquiggd commented Jan 17, 2021

Ah, I think I am on the right track:

In addition to defining the implementation of IObjectMother, which I am using to create an NSubstitute for an "IDataRepository" you also need to make that available, by referring to the target interface within the Given method.

So the ObjectMother is defined somewhere in the same assembly as the tests:

Option 1: Implement IObjectMother

    public class DataRepositoryMother : IObjectMother
    {
        public bool Applies(Type type) { return typeof(IDataRepository).IsAssignableFrom(type); }

        public object Create(Type type, IChillObjectResolver resolver)
        {
            var sub = Substitute.For<IDataRepository>();
            // set up the data to be returned by methods, as per standard NSusbstitute syntax
            return sub;
        }

        public bool IsFallback { get; } = true;
    }

Option 2: Use ObjectMother abstract class

    public class DataRepositoryMother : ObjectMother<IDataRepository>
    {
        protected override IDataRepositoryCreate()
        {            
             var sub = Substitute.For<IDataRepository>();
            // set up the data to be returned by methods, as per standard NSusbstitute syntax
            return sub;
        }
   }

And then, in the definition of the tests:

Given(() => The<IDataRepository>());

If this line is not included, AutoFac will not resolve the type.

Which makes sense. And is very clean.

My comment regarding the some clearer samples, to boost the popularity of this project, still stands - I'll have a stab at creating one myself, and post it for comment.

@dennisdoomen
Copy link
Collaborator

Hi David,

The EntityMother in that example is something external and just an example. Chill does have an IObjectMother abstraction, but its main purpose is to provide a mechanism to easily produce fakes and such by using your favorite mocking library. An example of that can be found here the (incomplete) Chill.FakeItEasy library. The idea is that you can use The<IMyObject>() without having to register it in the Given using UseThe or SetThe methods. But in reality, you usually do want to setup your test objects in a specific way and then object mothers are not going to help you.

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