{"action":"created","issue":{"url":"https://api.github.com/repos/AutoFixture/AutoFixture/issues/354","labels_url":"https://api.github.com/repos/AutoFixture/AutoFixture/issues/354/labels{/name}","comments_url":"https://api.github.com/repos/AutoFixture/AutoFixture/issues/354/comments","events_url":"https://api.github.com/repos/AutoFixture/AutoFixture/issues/354/events","html_url":"https://github.com/AutoFixture/AutoFixture/issues/354","id":59251412,"number":354,"title":"AutoFixture's AutoDataAttribute and xUnit's PropertyDataAttribute don't play nice together","user":{"login":"alexfoxgill","id":3819573,"avatar_url":"https://avatars.githubusercontent.com/u/3819573?v=3","gravatar_id":"","url":"https://api.github.com/users/alexfoxgill","html_url":"https://github.com/alexfoxgill","followers_url":"https://api.github.com/users/alexfoxgill/followers","following_url":"https://api.github.com/users/alexfoxgill/following{/other_user}","gists_url":"https://api.github.com/users/alexfoxgill/gists{/gist_id}","starred_url":"https://api.github.com/users/alexfoxgill/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alexfoxgill/subscriptions","organizations_url":"https://api.github.com/users/alexfoxgill/orgs","repos_url":"https://api.github.com/users/alexfoxgill/repos","events_url":"https://api.github.com/users/alexfoxgill/events{/privacy}","received_events_url":"https://api.github.com/users/alexfoxgill/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":8,"created_at":"2015-02-27T15:35:09Z","updated_at":"2015-03-01T20:02:39Z","closed_at":null,"body":"As described in [this StackOverflow answer](http://stackoverflow.com/a/18930198/1860652), the two attributes can't be combined in the usual ways.\r\n\r\nI have shared my solution [here](http://stackoverflow.com/a/28767887/1860652) - if you're interested in implementing this in AutoFixture.xUnit.net, I could get it into shape for a PR"},"comment":{"url":"https://api.github.com/repos/AutoFixture/AutoFixture/issues/comments/76627269","html_url":"https://github.com/AutoFixture/AutoFixture/issues/354#issuecomment-76627269","issue_url":"https://api.github.com/repos/AutoFixture/AutoFixture/issues/354","id":76627269,"user":{"login":"moodmosaic","id":287532,"avatar_url":"https://avatars.githubusercontent.com/u/287532?v=3","gravatar_id":"","url":"https://api.github.com/users/moodmosaic","html_url":"https://github.com/moodmosaic","followers_url":"https://api.github.com/users/moodmosaic/followers","following_url":"https://api.github.com/users/moodmosaic/following{/other_user}","gists_url":"https://api.github.com/users/moodmosaic/gists{/gist_id}","starred_url":"https://api.github.com/users/moodmosaic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/moodmosaic/subscriptions","organizations_url":"https://api.github.com/users/moodmosaic/orgs","repos_url":"https://api.github.com/users/moodmosaic/repos","events_url":"https://api.github.com/users/moodmosaic/events{/privacy}","received_events_url":"https://api.github.com/users/moodmosaic/received_events","type":"User","site_admin":false},"created_at":"2015-03-01T20:02:39Z","updated_at":"2015-03-01T20:02:39Z","body":"As part of the route cause analysis, I think it'd be beneficial to see how plain xUnit.net data theories work, and compare that with AutoFixture.Xunit's `CompositeDataAttribute` behaviour.\r\n\r\n### xUnit.net ##\r\n\r\nBelow is how xUnit.net behaves in a parameterized test using the Xunit.Extensions.PropertyDataAttribute:\r\n\r\n```csharp\r\npublic class MyClass\r\n{\r\n    public static IEnumerable<object[]> Letters\r\n    {\r\n        get\r\n        {\r\n            yield return new[] { \"a\" };\r\n            yield return new[] { \"b\" };\r\n        }\r\n    }\r\n\r\n    [Theory]\r\n    [Xunit.Extensions.PropertyData(\"Letters\")]\r\n    public void Test(string s1, string s2)\r\n    {\r\n        Assert.NotNull(s1);\r\n        Assert.NotNull(s2);\r\n    }\r\n}\r\n```\r\n\r\nThe above vanilla-xUnit.net parameterized test fails (with xUnit.net < 2.0) with the following exception:\r\n\r\n```\r\nTest 'Xyz.MyClass.Test(\"a\")' failed: System.InvalidOperationException\r\n  : Expected 2 parameters, got 1 parameters\r\n    at Xunit.Extensions.TheoryCommand.Execute(Object testClass)\r\n    at Xunit.Sdk.FixtureCommand.Execute(Object testClass)\r\n    at Xunit.Sdk.BeforeAfterCommand.Execute(Object testClass)\r\n    at Xunit.Sdk.LifetimeCommand.Execute(Object testClass)\r\n    at Xunit.Sdk.ExceptionAndOutputCaptureCommand.Execute(Object testClass)\r\n\r\nTest 'Xyz.MyClass.Test(\"b\")' failed: System.InvalidOperationException\r\n  : Expected 2 parameters, got 1 parameters\r\n    at Xunit.Extensions.TheoryCommand.Execute(Object testClass)\r\n    at Xunit.Sdk.FixtureCommand.Execute(Object testClass)\r\n    at Xunit.Sdk.BeforeAfterCommand.Execute(Object testClass)\r\n    at Xunit.Sdk.LifetimeCommand.Execute(Object testClass)\r\n    at Xunit.Sdk.ExceptionAndOutputCaptureCommand.Execute(Object testClass)\r\n\r\n0 passed, 2 failed, 0 skipped, took 0.50 seconds (xUnit.net 1.8.0 build 1549).\r\n```\r\n\r\nThis means that xUnit.net executes tests decorated with `[PropertyData(\"Letters\")]` twice, supplying:\r\n * `\"a\"` on the first run\r\n * `\"b\"` on the second run\r\n\r\nIn other words, xUnit.net sees the `Letters` property as:\r\n\r\n```csharp\r\npublic static IEnumerable<object[]> Letters\r\n{\r\n    get\r\n    {\r\n        yield return new[] { \"a\" }; // 1st run  \r\n        yield return new[] { \"b\" }; // 2nd run\r\n    }\r\n}\r\n```\r\n\r\n### AutoFixture.Xunit ###\r\n\r\nThe `CompositeDataAttribute ` also works that way, in the sense that it respects the way xUnit.net works with data theories.\r\n\r\nHypothesis: A test with 3 parameters, decorated with one attribute, which is composed of 2 attributes:\r\n\r\n```csharp\r\npublic class PropertyAutoDataAttribute : CompositeDataAttribute\r\n{\r\n    public PropertyAutoDataAttribute(string propertyName)\r\n        : base(\r\n            new DataAttribute[] \r\n                {\r\n                    new PropertyDataAttribute(propertyName), \r\n                    new AutoDataAttribute()\r\n                })\r\n    {\r\n    }\r\n}\r\n\r\npublic class MyClass\r\n{\r\n    public static IEnumerable<object[]> Letters\r\n    {\r\n        get\r\n        {\r\n            yield return new[] { \"a\" };\r\n            yield return new[] { \"b\" };\r\n        }\r\n    }\r\n\r\n    [Theory]\r\n    [PropertyAutoData(\"Letters\")]\r\n    public void Test(string s1, string s2, string s3)\r\n    {\r\n        Assert.NotNull(s1);\r\n        Assert.NotNull(s2);\r\n        Assert.NotNull(s3);\r\n    }\r\n}\r\n```\r\n\r\nThe above xUnit.net parameterized test fails (with xUnit.net < 2.0) with the following exception:\r\n\r\n```\r\nTest 'Xyz.MyClass.Test' failed: System.InvalidOperationException: An exception\r\nwas thrown while getting data for theory Xyz.MyClass.Test:\r\n\r\nSystem.InvalidOperationException: Expected 3 parameters, got 1 parameters\r\n   at Ploeh.AutoFixture.Xunit.CompositeDataAttribute.<GetData>d__0.MoveNext()\r\n    in ...\\CompositeDataAttribute.cs:line 148\r\n   at Xunit.Extensions.TheoryAttribute.<GetData>d__7.MoveNext()\r\n   at Xunit.Extensions.TheoryAttribute.EnumerateTestCommands(IMethodInfo method)\r\n    at Xunit.Extensions.TheoryAttribute.<>c__DisplayClass5\r\n      .<EnumerateTestCommands>b__1()\r\n    at Xunit.Extensions.TheoryAttribute.LambdaTestCommand\r\n      .Execute(Object testClass)\r\n```\r\n\r\nThis is expected, given the way xUnit.net works with data theories. The exception message is misleading, and that's something we could improve on our end.\r\n\r\n### Conclusion ###\r\nGiven all the above, it looks to me as the solution described in [this](http://stackoverflow.com/a/18930198/467754) Stack Overflow answer is still valid.\r\n\r\nWe could try to modify `CompositeDataAttribute` so that it *thinks* that there's no data theory iterations concept, but that would be different from the way xUnit.net works with data theories:\r\n\r\n```diff\r\ndiff --git a/../CompositeDataAttribute.cs b/../CompositeDataAttribute.cs\r\nindex 6044a41..377c302 100644\r\n--- a/../CompositeDataAttribute.cs\r\n+++ b/../CompositeDataAttribute.cs\r\n@@ -110,7 +110,7 @@ namespace Ploeh.AutoFixture.Xunit\r\n                         }\r\n                     }\r\n \r\n-                    var theory = attributeData[iteration];\r\n+                    var theory = attributeData.SelectMany(x => x).ToArray();\r\n \r\n                     int remaining =  numberOfParameters - foundData[iteration].Count;\r\n                     if (remaining == numberOfParameters)\r\n@@ -143,17 +143,8 @@ namespace Ploeh.AutoFixture.Xunit\r\n                 {\r\n                     yield return foundData[iteration].ToArray();\r\n                 }\r\n-                else\r\n-                {\r\n-                    throw new InvalidOperationException(\r\n-                          string.Format(\r\n-                              CultureInfo.CurrentCulture,\r\n-                              \"Expected {0} parameters, got {1} parameters\",\r\n-                              numberOfParameters, foundData[iteration].Count\r\n-                              )\r\n-                          );\r\n-                }\r\n-            } while (++iteration < numberOfIterations);\r\n+\r\n+            } while (foundData[iteration].Count < numberOfParameters);\r\n         }\r\n     }\r\n }\r\n```\r\n\r\nThen, the following test would pass:\r\n\r\n```csharp\r\npublic class PropertyAutoDataAttribute : CompositeDataAttribute\r\n{\r\n    public PropertyAutoDataAttribute(string propertyName)\r\n        : base(\r\n            new DataAttribute[] \r\n            {\r\n                new PropertyDataAttribute(propertyName), \r\n                new AutoDataAttribute()\r\n            })\r\n    {\r\n    }\r\n}\r\n\r\npublic class MyClass\r\n{\r\n    public static IEnumerable<object[]> Letters\r\n    {\r\n        get\r\n        {\r\n            yield return new[] { \"a\" };\r\n            yield return new[] { \"b\" };\r\n        }\r\n    }\r\n\r\n    [Theory]\r\n    [PropertyAutoData(\"Letters\")]\r\n    public void Test(string s1, string s2, string s3)\r\n    {\r\n        Assert.NotNull(s1);\r\n        Assert.NotNull(s2);\r\n        Assert.NotNull(s3);\r\n    }\r\n}\r\n```\r\n\r\nHowever, it would be a lot easier if we leave `CompositeDataAttribute` as-is, makes things more explicit, and define another Composite for xUnit.net data attributes, which behaves that way."}}