Skip to content

Instantly share code, notes, and snippets.

@thoolihan
Created March 21, 2013 01:12
Show Gist options
  • Save thoolihan/5209939 to your computer and use it in GitHub Desktop.
Save thoolihan/5209939 to your computer and use it in GitHub Desktop.
Example C# Unit Test
using System.Linq;
using Kryptos.Model;
using Kryptos.Model.Tests;
using Kryptos.Presentation.Property;
using NUnit.Framework;
using Rhino.Mocks;
using Should.Fluent;
namespace Kryptos.Presentation.Test.Property
{
[TestFixture]
public class PropertyDetailPresenterTest
{
private MockRepository _mocks;
private PropertyDetailPresenter _presenter;
private Model.Property _property;
private PropertyAddress _propertyAddress;
private Model.User _user;
private Model.Violation _violation;
protected IPropertyDetailView View{ get; set; }
protected PresenterTestStubs Stubs { get; set; }
[SetUp]
public void SetUp()
{
_mocks = new MockRepository();
Stubs = new PresenterTestStubs(_mocks);
View = _mocks.Stub<IPropertyDetailView>();
_propertyAddress = TestDataGenerator.CreatePropertyAddress(true);
_property = _propertyAddress.Property;
_violation = TestDataGenerator.CreateViolation();
_property.Violations.Add(_violation);
_user = TestDataGenerator.CreateHOAUser();
_presenter = new PropertyDetailPresenter(View, Stubs.PropertyService, Stubs.UserService);
Stubs.PropertyService.Stub(x => x.GetDisplayedInterestedParties(null, null)).IgnoreArguments().Return(_property.Interests);
Stubs.PropertyService.Stub(x => x.GetDisplayedViolations(null, null)).IgnoreArguments().Return(_property.Violations);
Stubs.PropertyService.Stub(x => x.FindById(1)).Return(_property);
View.Stub(x => x.PopulateFullLink("")).IgnoreArguments().Return("");
View.Stub(x => x.HideMersData());
View.Stub(x => x.HidePowerREOData());
View.Stub(x => x.HideHSBCData());
View.Stub(x => x.HideAHMSIData());
}
[Test]
public void Retrieves_an_existing_property_and_binds_to_the_view()
{
View.Stub(x => x.CurrentUser).Return(_user);
StubGetInterestedParty();
Stubs.PropertyService.Stub(x => x.UserHasAccessToProperty(null, null)).IgnoreArguments().Return(true);
Stubs.PropertyService.Stub(x => x.GetInterestedPartyForUser(null, null)).IgnoreArguments().Return(null);
_mocks.ReplayAll();
_presenter.Display(1);
Assert.AreEqual(_property.ParcelId, View.RecordId);
Assert.AreEqual(_propertyAddress.Address.FullAddress, View.FullAddress);
Assert.AreEqual(_propertyAddress.Address.FullStreetAddress, View.StreetAddress);
Assert.AreEqual(_propertyAddress.Address.City, View.City);
Assert.AreEqual(_propertyAddress.Address.State, View.State);
Assert.AreEqual(_propertyAddress.Address.Zip, View.ZipCode);
Assert.AreEqual(_propertyAddress.Address.County.Name, View.County);
Assert.IsTrue(View.PropertyImageUrl.Contains(_propertyAddress.Address.FullAddress));
Assert.AreEqual(_property.Notes, View.Notes);
}
[Test]
public void Displays_ownership_data_when_user_has_access()
{
View.Stub(x => x.CurrentUser).Return(_user);
StubGetInterestedParty();
Stubs.PropertyService.Stub(x => x.UserHasAccessToProperty(null, null)).IgnoreArguments().Return(true);
Stubs.PropertyService.Stub(x => x.GetInterestedPartyForUser(null, null)).IgnoreArguments().Return(null);
_mocks.ReplayAll();
_presenter.Display(1);
Assert.AreEqual(_property.Subdivision, View.Subdivision);
Assert.AreEqual(_property.AccountNumber, View.AccountNumber);
Assert.IsTrue(View.SubmitViolationVisible);
Assert.AreEqual(_property.Interests.First().Party.Name, View.InterestedParties.First().Party.Name);
Assert.AreEqual(_property.Interests.First().Party.PrimaryContactName, View.InterestedParties.First().Party.PrimaryContactName);
Assert.AreEqual(_property.Interests.First().Party.PrimaryContactPrimaryPhone, View.InterestedParties.First().Party.PrimaryContactPrimaryPhone);
Assert.AreEqual(_property.Interests.First().Party.PrimaryContactEmail.Address, View.InterestedParties.First().Party.PrimaryContactEmail.Address);
Assert.AreEqual(_property.Interests.First().FinancialInterest.LoanNumber, View.InterestedParties.First().FinancialInterest.LoanNumber);
}
[Test]
public void Displays_submit_violation_for_bank_user()
{
var bankUser = TestDataGenerator.CreateBankUser();
View.Stub(x => x.CurrentUser).Return(bankUser);
StubGetInterestedParty();
Stubs.PropertyService.Stub(x => x.UserHasAccessToProperty(null, null)).IgnoreArguments().Return(true);
Stubs.PropertyService.Stub(x => x.GetInterestedPartyForUser(null, null)).IgnoreArguments().Return(null);
_mocks.ReplayAll();
_presenter.Display(1);
Assert.IsTrue(View.SubmitViolationVisible);
}
[Test]
public void Displays_violation_data_when_user_has_access()
{
View.Stub(x => x.CurrentUser).Return(_user);
StubGetInterestedParty();
Stubs.PropertyService.Stub(x => x.UserHasAccessToProperty(null, null)).IgnoreArguments().Return(true);
Stubs.PropertyService.Stub(x => x.GetInterestedPartyForUser(null, null)).IgnoreArguments().Return(null);
_mocks.ReplayAll();
_presenter.Display(1);
Assert.AreEqual(_property.Violations.First().Description, View.Violations.First().Description);
Assert.AreEqual(_property.Violations.First().Id, View.Violations.First().Id);
Assert.AreEqual(_property.Violations.First().ViolationType.Type, View.Violations.First().ViolationType.Type);
}
[Test]
public void Replaces_newline_with_break_in_notes_field()
{
_property.Notes = "This is\r\n a string\r\n with newlines";
View.Stub(x => x.CurrentUser).Return(_user);
StubGetInterestedParty();
Stubs.PropertyService.Stub(x => x.UserHasAccessToProperty(null, null)).IgnoreArguments().Return(true);
Stubs.PropertyService.Stub(x => x.GetInterestedPartyForUser(null, null)).IgnoreArguments().Return(null);
_mocks.ReplayAll();
_presenter.Display(1);
Assert.AreEqual(_property.Notes.Replace("\r\n", "<br />\r\n"), View.Notes);
}
[Test]
public void Hides_purchase_options_if_user_has_access_to_property()
{
View.Stub(x => x.CurrentUser).Return(_user);
StubGetInterestedParty();
Stubs.PropertyService.Stub(x => x.UserHasAccessToProperty(null, null)).IgnoreArguments().Return(true);
Stubs.PropertyService.Stub(x => x.GetInterestedPartyForUser(null, null)).IgnoreArguments().Return(_property.Interests.First());
_mocks.ReplayAll();
_presenter.Display(1);
Assert.IsTrue(View.NotesSectionVisible);
}
[Test]
public void Displays_purchase_options_if_user_does_not_have_access_to_property()
{
View.Stub(x => x.CurrentUser).Return(_user);
StubGetInterestedParty();
Stubs.PropertyService.Stub(x => x.UserHasAccessToProperty(null, null)).IgnoreArguments().Return(false);
Stubs.PropertyService.Stub(x => x.GetInterestedPartyForUser(null, null)).IgnoreArguments().Return(null);
_mocks.ReplayAll();
_presenter.Display(1);
Assert.IsFalse(View.NotesSectionVisible);
}
[Test]
public void Hides_processed_by_for_pending_property()
{
_property.Status = PropertyStatus.Pending;
View.Stub(x => x.CurrentUser).Return(_user);
StubGetInterestedParty();
Stubs.PropertyService.Stub(x => x.UserHasAccessToProperty(null, null)).IgnoreArguments().Return(false);
Stubs.PropertyService.Stub(x => x.GetInterestedPartyForUser(null, null)).IgnoreArguments().Return(null);
_mocks.ReplayAll();
_presenter.Display(1);
View.ProcessedByVisible.Should().Be.False();
}
[Test]
public void Shows_approved_by_for_approved_property()
{
_property.Status = PropertyStatus.Approved;
View.Stub(x => x.CurrentUser).Return(_user);
StubGetInterestedParty();
Stubs.PropertyService.Stub(x => x.UserHasAccessToProperty(null, null)).IgnoreArguments().Return(false);
Stubs.PropertyService.Stub(x => x.GetInterestedPartyForUser(null, null)).IgnoreArguments().Return(null);
_mocks.ReplayAll();
_presenter.Display(1);
View.ProcessedByVisible.Should().Be.True();
View.ProcessedLabelKey.Should().Equal("ApprovedBy");
}
[Test]
public void Shows_rejected_by_for_rejected_property()
{
_property.Status = PropertyStatus.Rejected;
View.Stub(x => x.CurrentUser).Return(_user);
StubGetInterestedParty();
Stubs.PropertyService.Stub(x => x.UserHasAccessToProperty(null, null)).IgnoreArguments().Return(false);
Stubs.PropertyService.Stub(x => x.GetInterestedPartyForUser(null, null)).IgnoreArguments().Return(null);
_mocks.ReplayAll();
_presenter.Display(1);
View.ProcessedByVisible.Should().Be.True();
View.ProcessedLabelKey.Should().Equal("RejectedBy");
}
[Test]
public void Hides_violation_submittion_and_interest_creation_for_rejected_property()
{
_property.Status = PropertyStatus.Rejected;
View.Stub(x => x.CurrentUser).Return(_user);
StubGetInterestedParty();
Stubs.PropertyService.Stub(x => x.UserHasAccessToProperty(null, null)).IgnoreArguments().Return(false);
Stubs.PropertyService.Stub(x => x.GetInterestedPartyForUser(null, null)).IgnoreArguments().Return(null);
_mocks.ReplayAll();
_presenter.Display(1);
View.ProcessSearchVisible.Should().Be.False();
View.SubmitViolationVisible.Should().Be.False();
}
public void StubGetInterestedParty(InterestedParty interestedParty = null)
{
Stubs.PropertyService.Stub(x => x.GetInterestedPartyForUser(null, null)).IgnoreArguments().Return(interestedParty);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment