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

AutoFixture silently totally ignores ObservableCollection properties (sample code inside) #191

Closed
Seikilos opened this issue Oct 10, 2013 · 6 comments
Labels

Comments

@Seikilos
Copy link

I am on 3.10.1 and my first contact with AutoFixture just failed :/
Does AF has issues with ObservableCollections?

Following class is created but the ObservableCollection is empty whereas the List is populated just fine:

public class Foo
{
  public ObservableCollection<string> Observables { get; set; }
  public IList<string> Lists { get; set; }
}

public void Test()
{
  var f = new Fixture();

  var foo = f.Create<Foo>();
}

Result is 3 entries in Lists but 0 in Observables.

I've also tried Build().Do(x => f.AddMany(...));

@moodmosaic
Copy link
Member

Currently there is no support for ObservableCollection<T> (that could change in a future release though..)


Here is how you can manually configure AutoFixture to fill collections of type ObservableCollection<T>:

fixture.Customizations.Add(
    new FilteringSpecimenBuilder(
        new MethodInvoker(
            new EnumerableFavoringConstructorQuery()),
        new ObservableCollectionSpecification()));

The ObservableCollectionSpecification can be defined as:

internal class ObservableCollectionSpecification : IRequestSpecification
{
    public bool IsSatisfiedBy(object request)
    {
        var type = request as Type;
        if (type == null)
            return false;

        return type.IsGenericType
            && typeof(ObservableCollection<>) == type.GetGenericTypeDefinition();
    }
}

@Seikilos
Copy link
Author

Thanks for the sample. I'll try it tomorrow.
Why is there no direct support for it if there is a way to implement it? Some other constraints?

@moodmosaic
Copy link
Member

No, not that I can think of. Actually it sounds like a good idea to add support for ObservableCollection<T> which is also part of the BCL.

This class is inside the System.Collections namespace which is one of the namespaces that are standardized as of the ECMA 335 and ISO/IEC 23271:2006 standards. (source)

@moodmosaic
Copy link
Member

Did the provided sample work for you?

@Seikilos
Copy link
Author

Yes. It did indeed work.
I awaited your comment before I close it. I was just curious if all that was needed to be done to enable AF for ObservableCollection is the sample posted by you, then what could prevent this sample from making it into a official AF release.

Thanks again.

@moodmosaic
Copy link
Member

AutoFixture now officially supports ObservableCollection<T> thanks to @adamchester!

That means when you update to AutoFixture 3.11.0 your original test will work out of the box :)

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

No branches or pull requests

2 participants