Skip to content

Commit

Permalink
- Add AAA implementation on every tests where it was possible
Browse files Browse the repository at this point in the history
  • Loading branch information
FabienArcellier committed Dec 8, 2009
1 parent d227148 commit 47f74d6
Show file tree
Hide file tree
Showing 19 changed files with 1,707 additions and 1,238 deletions.
@@ -1,12 +1,12 @@
namespace RhinoMocksIntroduction
{
/// <summary>
/// Minimum implementation to pass the test
/// </summary>
public interface IProjectView
{
string Title { get; set; }
bool HasChanges { get; set; }
string Ask(string arg1, string arg2);
}
}
namespace RhinoMocksIntroduction
{
/// <summary>
/// Minimum implementation to pass the test
/// </summary>
public interface IProjectView
{
string Title { get; set; }
bool HasChanges { get; set; }
string Ask(string arg1, string arg2);
}
}
@@ -1,28 +1,29 @@
namespace RhinoMocksIntroduction
{
/// <summary>
/// Minimum implementation to pass the test
/// </summary>
public class ProjectPresenter : IProjectPresenter
{
public IProject m_Prj;
public IProjectView m_View;

public ProjectPresenter(IProject prj, IProjectView view)
{
this.m_Prj = prj;
this.m_View = view;
}

#region IProjectPresenter Members

bool IProjectPresenter.SaveProjectAs()
{
string projectName = m_View.Title;
m_View.Ask(null, null);
return false;
}

#endregion
}
}
using System;
namespace RhinoMocksIntroduction
{
/// <summary>
/// Minimum implementation to pass the test
/// </summary>
public class ProjectPresenter : IProjectPresenter
{
public IProject m_Prj;
public IProjectView m_View;

public ProjectPresenter(IProject prj, IProjectView view)
{
this.m_Prj = prj;
this.m_View = view;
}

#region IProjectPresenter Members

bool IProjectPresenter.SaveProjectAs()
{
string projectName = m_View.Title;
m_View.Ask(null, null);
return false;
}

#endregion
}
}
@@ -1,60 +1,111 @@
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Rhino.Mocks;
using Rhino.Mocks.Exceptions;

namespace RhinoMocksIntroduction
{
/// <summary>
/// The purpose of mock objects is to allow you
/// to test the interactions between components,
/// this is very useful when you test code
/// that doesn't lend itself easily to state based testing.
///
/// Most of the examples here are tests that check the save routine for a view
/// to see if it is working as expected.
/// </summary>
/// <see cref="http://www.ayende.com/wiki/Rhino+Mocks+Introduction.ashx"/>
public class RhinoMocksIntroductionTest
{
[Test]
public void SaveProjectAs_CanBeCanceled()
{
MockRepository mocks = new MockRepository();
IProjectView projectView = mocks.StrictMock<IProjectView>();

Project prj = new Project("Example Project");
IProjectPresenter presenter = new ProjectPresenter(prj, projectView);
Expect.Call(projectView.Title).Return(prj.Name);
Expect.Call(projectView.Ask(null, null)).Return(null);

mocks.ReplayAll();
Assert.IsFalse(presenter.SaveProjectAs());
mocks.VerifyAll();
}

[Test]
[ExpectedException(typeof(ExpectationViolationException),
ExpectedMessage = "IDemo.VoidNoArgs(); Expected #0, Actual #1.")]
public void MockObjectThrowsForUnexpectedCall()
{
MockRepository mocks = new MockRepository();
IDemo demo = mocks.StrictMock<IDemo>();
mocks.ReplayAll();
demo.VoidNoArgs();
mocks.VerifyAll();//will never get here
}

[Test]
public void DyamicMockAcceptUnexpectedCall()
{
MockRepository mocks = new MockRepository();
IDemo demo = mocks.DynamicMock<IDemo>();
mocks.ReplayAll();
demo.VoidNoArgs();
mocks.VerifyAll();//works like a charm
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Rhino.Mocks;
using Rhino.Mocks.Exceptions;

namespace RhinoMocksIntroduction
{
/// <summary>
/// The purpose of mock objects is to allow you
/// to test the interactions between components,
/// this is very useful when you test code
/// that doesn't lend itself easily to state based testing.
///
/// Most of the examples here are tests that check the save routine for a view
/// to see if it is working as expected.
/// </summary>
/// <see cref="http://www.ayende.com/wiki/Rhino+Mocks+Introduction.ashx"/>
public class RhinoMocksIntroductionTest
{
[Test]
public void SaveProjectAs_CanBeCanceled()
{
MockRepository mocks = new MockRepository();
IProjectView view = mocks.StrictMock<IProjectView>();
Project prj = new Project("Example Project");
IProjectPresenter presenter = new ProjectPresenter(prj, view);

Expect.Call(view.Title).Return(prj.Name);
Expect.Call(view.Ask(null, null)).Return("a");

mocks.ReplayAll();
Assert.IsFalse(presenter.SaveProjectAs());
mocks.VerifyAll();
}

/// <summary>
/// This Test is the same than above using Arrange/Act/Assert pattern
/// </summary>
[Test]
public void SaveProjectAs_CanBeCanceled_AAA()
{
//Arrange
IProjectView view = MockRepository.GenerateStrictMock<IProjectView>();
Project prj = new Project("Example Project");
IProjectPresenter presenter = new ProjectPresenter(prj, view);

view.Expect(v => v.Title).Return(null);
view.Expect(v => v.Ask(Arg<string>.Is.Null, Arg<string>.Is.Null)).Return(null);

//Act
bool isProjectSave = presenter.SaveProjectAs();

//Assert
Assert.IsFalse(isProjectSave);
view.VerifyAllExpectations();
}

[Test]
[ExpectedException(typeof(ExpectationViolationException),
ExpectedMessage = "IDemo.VoidNoArgs(); Expected #0, Actual #1.")]
public void MockObjectThrowsForUnexpectedCall()
{
MockRepository mocks = new MockRepository();
IDemo demo = mocks.StrictMock<IDemo>();
mocks.ReplayAll();
demo.VoidNoArgs();
mocks.VerifyAll();//will never get here
}

[Test]
[ExpectedException(typeof(ExpectationViolationException),
ExpectedMessage = "IDemo.VoidNoArgs(); Expected #0, Actual #1.")]
public void MockObjectThrowsForUnexpectedCall_AAA()
{
//Arrange
IDemo demo = MockRepository.GenerateStrictMock<IDemo>();

//Act
demo.VoidNoArgs();

//Assert is a different, we expect an exception
}

[Test]
public void DyamicMockAcceptUnexpectedCall()
{
MockRepository mocks = new MockRepository();
IDemo demo = mocks.DynamicMock<IDemo>();

mocks.ReplayAll();
demo.VoidNoArgs();
Assert.Pass();
mocks.VerifyAll();//works like a charm
}

[Test]
public void DyamicMockAcceptUnexpectedCall_AAA()
{
//Arrange
IDemo demo = MockRepository.GenerateMock<IDemo>();

//Act
demo.VoidNoArgs();

//Assert
Assert.Pass();
}
}
}
@@ -1,30 +1,46 @@
using System.Collections.Generic;
using NUnit.Framework;
using Rhino.Mocks;

namespace RhinoMocksGenerics
{
/// <summary>
/// While Rhino Mocks is compatible with the .Net framework 1.1,
/// it offer several goodies for those who use the 2.0 version of the .Net framework.
///
/// Among them are mocking generic interfaces and classes,
/// and using generic version of the methods in order to reduce casting.
/// </summary>
/// <see cref="http://www.ayende.com/wiki/Rhino+Mocks+Generics.ashx"/>
[TestFixture]
public class RhinoMocksGenericsTest
{
[Test]
public void MockAGenericInterface()
{
MockRepository mocks = new MockRepository();
IList<int> list = mocks.StrictMock<IList<int>>();
Assert.IsNotNull(list);
Expect.Call(list.Count).Return(5);
mocks.ReplayAll();
Assert.AreEqual(5, list.Count);
mocks.VerifyAll();
}
}
}
using System.Collections.Generic;
using NUnit.Framework;
using Rhino.Mocks;

namespace RhinoMocksGenerics
{
/// <summary>
/// While Rhino Mocks is compatible with the .Net framework 1.1,
/// it offer several goodies for those who use the 2.0 version of the .Net framework.
///
/// Among them are mocking generic interfaces and classes,
/// and using generic version of the methods in order to reduce casting.
/// </summary>
/// <see cref="http://www.ayende.com/wiki/Rhino+Mocks+Generics.ashx"/>
[TestFixture]
public class RhinoMocksGenericsTest
{
[Test]
public void MockAGenericInterface()
{
MockRepository mocks = new MockRepository();
IList<int> list = mocks.StrictMock<IList<int>>();

Expect.Call(list.Count).Return(5);

mocks.ReplayAll();
Assert.AreEqual(5, list.Count);
mocks.VerifyAll();
}

[Test]
public void MockAGenericInterface_AAA()
{
//Arrange
IList<int> list = MockRepository.GenerateStrictMock<IList<int>>();
list.Expect(l => l.Count).Return(5);

//Act
int count = list.Count;

//Assert
Assert.AreEqual(5, count);
list.VerifyAllExpectations();
}
}
}

0 comments on commit 47f74d6

Please sign in to comment.