Skip to content

Instantly share code, notes, and snippets.

@DarranShepherd
Last active August 29, 2015 14:09
Show Gist options
  • Save DarranShepherd/39225bf32eb3655854d8 to your computer and use it in GitHub Desktop.
Save DarranShepherd/39225bf32eb3655854d8 to your computer and use it in GitHub Desktop.
public abstract class MyBaseClass { }
public class DerivedClass : MyBaseClass { }
public class SubjectClass
{
private readonly IGenericFoo foo;
public SubjectClass(IGenericFoo foo)
{
this.foo = foo;
}
public object GetObjectFromType(object input)
{
return this.GetType().GetMethod("GetObjectFrom").MakeGenericMethod(input.GetType()).Invoke(this, new[] { input });
}
public object GetObjectFrom<T>(T input)
{
return this.foo.GetObjectFrom<T>();
}
}
public interface IGenericFoo
{
object GetObjectFrom<T>();
}
public class Test : WithSubject<SubjectClass>
{
static MyBaseClass input;
static object o;
Establish context = () => input = new DerivedClass();
Because of = () => o = Subject.GetObjectFromType(input);
It should_have_called_with_derived = () => The<IGenericFoo>().WasToldTo(f => f.GetObjectFrom<DerivedClass>());
It should_not_have_called_with_base = () => The<IGenericFoo>().WasNotToldTo(f => f.GetObjectFrom<MyBaseClass>());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment