-
-
Save AlexZeitler/927605 to your computer and use it in GitHub Desktop.
| public class ListOfAValidAndAInvalidRequirement { | |
| OnEstablish context = | |
| fakeAccessor => | |
| { | |
| IRequirement validRequirement = fakeAccessor.An<IRequirement>(); | |
| validRequirement.WhenToldTo(r => r.Validate()).Return(true); // throws ex here | |
| IRequirement invalidRequirement = fakeAccessor.An<IRequirement>(); | |
| invalidRequirement.WhenToldTo(r=>r.Validate()).Return(false); | |
| IList<IRequirement> requirements = new List<IRequirement> | |
| { | |
| validRequirement, | |
| invalidRequirement | |
| }; | |
| fakeAccessor.Configure(config => config.For<IList<IRequirement>>().Use(()=>requirements)); | |
| }; | |
| } | |
| public interface IRequirement { | |
| bool Validate(); | |
| } | |
| throws "Can't create mocks of sealed classes" |
| the complete spec: | |
| public class When_validating_a_valid_requirement_and_a_invalid_requirement : WithSubject<RequirementsValidator> | |
| { | |
| static bool _isValid; | |
| Establish context = () => { With<ListOfAValidAndAInvalidRequirement>(); }; | |
| Because of = () => { _isValid = Subject.Validate(); }; | |
| It should_not_be_valid = () => { _isValid.ShouldBeFalse(); }; | |
| } | |
| public class ListOfAValidAndAInvalidRequirement { | |
| OnEstablish context = | |
| fakeAccessor => | |
| { | |
| IRequirement validRequirement = fakeAccessor.An<IRequirement>(); | |
| validRequirement.WhenToldTo(r => r.Validate()).Return(true); | |
| IRequirement invalidRequirement = fakeAccessor.An<IRequirement>(); | |
| invalidRequirement.WhenToldTo(r=>r.Validate()).Return(false); | |
| IList<IRequirement> requirements = new List<IRequirement> | |
| { | |
| validRequirement, | |
| invalidRequirement | |
| }; | |
| fakeAccessor.Configure(config => config.For<IList<IRequirement>>().Use(()=>requirements)); | |
| }; | |
| } | |
| public class RequirementsValidator { | |
| readonly IList<IRequirement> _requirements; | |
| public RequirementsValidator(IList<IRequirement> requirements) { | |
| _requirements = requirements; | |
| } | |
| public bool Validate() { | |
| return _requirements.Where(r => r.Validate()).Count() == _requirements.Count; | |
| } | |
| } | |
| public interface IRequirement { | |
| bool Validate(); | |
| } |
- Which flavor? Still Rhino?
Rhino - What do you see in the stacktrace? Does it come from the fake framework?
at Rhino.Mocks.MockRepository.MockClass(CreateMockState mockStateFactory, Type type, Type[] extras, Object[] argumentsForConstructor) - Does it really happen on the WhenToldTo line? This line shouldn't create any mock at all !!!
yes it's thrown there - that's why I'm confused
added the complete spec
Moq shows similar behavior. At least test fails and no mocks created.
So, you want to hear the good or the bad news? First of all the exception doesn't occur on my machine on WhenToldTo (is that the good news or the bad news ?!)
I've been running your sample against the current code base just a few minutes ago. There's a deeper problem with StructureMap in there though: StructureMap ignores registrations for IEnumerable<T> and IList<T>. If a collection of such a type is requested in SM it searches its plugin configuration for all plugin types for the requested interface and returns them instead. With a bare SM you can work around this by registering the dependencies explicitly with different names (For(typeof(IRequirement).Use(invalidRequirement).Named("invalidRequirement")). However this is not exposed in the MFakes API, nor would I have a good feeling about exposing such a functionality for such cases. It seems to container specific.
As a workaround I would suggest simply to create the Subject for that case by hand and don't rely on BehaviorConfigs and Configure ....
Ok, I'm going to try that with the merged mfakes assembly
While not getting the exception - does the test turn red or green?
Thanks for investigation, though.
Will have to dig into a test I wrote yesterday against the latest code base where I also used Configure and IList in BehaviorConfigs - weird stuff ;-)
So in all cases I tried Moq/Rhino/FakeItEasy --> merged/unmerged I didn't run into the exception. The specification fails because IList<IRequirement> contains no elements (explanation above ;-().
Maybe you did enable "Just my Code" in Debugging options?
This was enabled, however disabling it didn't change the outcome
Ok, could isolate it a little bit more:
using:
Subject = new RequirementsValidator(The<IList<IRequirement>>());
everything works fine - including BCs and Configure from above code
When letting Machine.Fakes inject The<IList<IRequirement>>() the exception is thrown.
Hard to diagnose from the far. Could you supply more infos?