Created
March 26, 2012 11:43
-
-
Save Rookian/2204561 to your computer and use it in GitHub Desktop.
FakeItEasyAbstractClassAndVirtualMembers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Subject(typeof(Presenter<>))] | |
public class When_disposing_a_presenter | |
{ | |
static IUnitOfWork UnitOfWork; | |
static Presenter<IView> Presenter; | |
static IView View; | |
Establish context = () => | |
{ | |
UnitOfWork = A.Fake<IUnitOfWork>(); | |
View = A.Fake<IView>(); | |
Presenter = A.Fake<Presenter<IView>>(); //new TestPresenter(View, UnitOfWork); | |
}; | |
Because of = () => Presenter.Dispose(); | |
It should_dispose_the_unit_of_work = () => A.CallTo(() => UnitOfWork.Dispose()).MustHaveHappened(); | |
class TestPresenter : Presenter<IView> | |
{ | |
public TestPresenter(IView currentView, IUnitOfWork unitOfWork) : base(currentView, unitOfWork) { } | |
} | |
} | |
public interface IUnitOfWork : IDisposable | |
{ | |
void Commit(); | |
void RollBack(); | |
} | |
public abstract class Presenter<TView> : IPresenter<TView> where TView : class | |
{ | |
readonly TView _currentView; | |
readonly IUnitOfWork _unitOfWork; | |
protected Presenter(TView currentView, IUnitOfWork unitOfWork) | |
{ | |
_currentView = currentView; | |
_unitOfWork = unitOfWork; | |
Ensure.That(currentView).IsNotNull(); | |
Ensure.That(unitOfWork).IsNotNull(); | |
} | |
public TView CurrentView | |
{ | |
get { return _currentView; } | |
} | |
public virtual void Dispose() | |
{ | |
_unitOfWork.Dispose(); | |
} | |
protected void ShowDialog(IView newForm, object parent) | |
{ | |
newForm.ShowDialog((IWin32Window) parent); | |
} | |
} | |
public interface IPresenter<out TView> : IDisposable | |
{ | |
TView CurrentView { get; } | |
} | |
public interface IView : IDisposable | |
{ | |
event EventHandler CloseClick; | |
void Close(); | |
DialogResult ShowDialog(IWin32Window parent); | |
} |
I need an instance of the abstract class. Do I have to use my TestPresenter instead? I thought I could use FakeItEasy to get an instance of my presenter.
This would be my solution by now:
[Subject(typeof(Presenter<>))]
public class When_disposing_a_presenter
{
static IUnitOfWork UnitOfWork;
static Presenter<IView> Presenter;
static IView View;
Establish context = () =>
{
UnitOfWork = A.Fake<IUnitOfWork>();
View = A.Fake<IView>();
Presenter = new TestPresenter(View, UnitOfWork);
};
Because of = () => Presenter.Dispose();
It should_dispose_the_unit_of_work = () => A.CallTo(() => UnitOfWork.Dispose()).MustHaveHappened();
class TestPresenter : Presenter<IView> { public TestPresenter(IView currentView, IUnitOfWork unitOfWork) : base(currentView, unitOfWork) { } }
}
Yeah, that's way better imho.
OK, thank you Björn :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why do you fake the subject of the specification? This doesn't make sense imho. Leave the subject under specification as-is and only stub/fake the dependencies (which would be IUnitOfWork and ISession) in that case.