Skip to content

Instantly share code, notes, and snippets.

@Rookian
Created March 26, 2012 11:43
Show Gist options
  • Save Rookian/2204561 to your computer and use it in GitHub Desktop.
Save Rookian/2204561 to your computer and use it in GitHub Desktop.
FakeItEasyAbstractClassAndVirtualMembers
[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);
}
@Rookian
Copy link
Author

Rookian commented Mar 26, 2012

2nd try does not work too:

[Subject(typeof(Presenter<>))]
public class When_disposing_a_presenter
{
    static IUnitOfWork UnitOfWork;
    static Presenter<IView> Presenter;
    static IView View;
    static ISession Session;

    Establish context = () =>
                            {
                                Session = A.Fake<ISession>();
                                UnitOfWork = A.Fake<UnitOfWork>(a => a.WithArgumentsForConstructor(new[] { Session }));
                                View = A.Fake<IView>();
                                Presenter = A.Fake<Presenter<IView>>();
                            };

    Because of = () => Presenter.Dispose();

    It should_dispose_the_unit_of_work = () => A.CallTo(() => UnitOfWork.Dispose()).MustHaveHappened();
}

FakeItEasy.Configuration.FakeConfigurationException:

The current proxy generator can not intercept the specified method for the following reason:
- Sealed methods can not be intercepted.

@BjRo
Copy link

BjRo commented Mar 26, 2012

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.

@Rookian
Copy link
Author

Rookian commented Mar 26, 2012

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) { } }
}

@BjRo
Copy link

BjRo commented Mar 26, 2012

Yeah, that's way better imho.

@Rookian
Copy link
Author

Rookian commented Mar 26, 2012

OK, thank you Björn :)

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