Skip to content

Instantly share code, notes, and snippets.

@MattHoneycutt
Created December 12, 2011 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattHoneycutt/1464500 to your computer and use it in GitHub Desktop.
Save MattHoneycutt/1464500 to your computer and use it in GitHub Desktop.
SpecsFor.FAQ
public class when_creating_the_SUT_manually : SpecsFor<ReallyComplexAndHardToCreateType>
{
protected override void InitializeClassUnderTest()
{
SUT = ReallyComplexAndHardToCreateType.CreateMyType("some", "params", 536);
}
...
}
public class when_injecting_into_a_service_that_depends_on_IEnumerable : SpecsFor<ConsumeEnumerableService>
{
private IEnumerable<IWidget> _testWidgets;
protected override void ConfigureContainer(StructureMap.IContainer container)
{
base.ConfigureContainer(container);
container.Inject(typeof (string), "blah");
var mocks = GetMockForEnumerableOf<IWidget>(10);
var widgets = new IWidget[10];
for (var i = 0; i < mocks.Length; i++)
{
var widget = mocks[i];
widget.Setup(w => w.Name).Returns("Widget " + i);
widgets[i] = widget.Object;
}
_testWidgets = widgets;
}
[Test]
public void then_it_provides_the_enumerable()
{
SUT.Widgets.Count().ShouldEqual(_testWidgets.Count());
}
[Test]
public void then_expectations_set_up_on_the_mocks_are_preserved()
{
SUT.Widgets.All(w => w.Name.StartsWith("Widget ")).ShouldBeTrue();
}
[Test]
public void then_I_can_retrieve_the_equivalent_mock_enumerable()
{
var mocks = GetMockForEnumerableOf<IWidget>(10);
var injectedNames = SUT.Widgets.Select(w => w.Name);
var mockNames = mocks.Select(m => m.Object.Name);
mockNames.ShouldEqual(injectedNames);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment