Skip to content

Instantly share code, notes, and snippets.

@AlexZeitler
Created April 19, 2011 13:16
Show Gist options
  • Select an option

  • Save AlexZeitler/927605 to your computer and use it in GitHub Desktop.

Select an option

Save AlexZeitler/927605 to your computer and use it in GitHub Desktop.
Can't create mocks of sealed classes
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();
}
@BjRo

BjRo commented Apr 19, 2011

Copy link
Copy Markdown

Ok, I'm going to try that with the merged mfakes assembly

@AlexZeitler

Copy link
Copy Markdown
Author

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 ;-)

@BjRo

BjRo commented Apr 19, 2011

Copy link
Copy Markdown

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 ;-().

@AlexZeitler

Copy link
Copy Markdown
Author

Maybe you did enable "Just my Code" in Debugging options?

@BjRo

BjRo commented Apr 19, 2011

Copy link
Copy Markdown

This was enabled, however disabling it didn't change the outcome

@AlexZeitler

Copy link
Copy Markdown
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment