Created
September 15, 2012 14:08
-
-
Save davybrion/3728116 to your computer and use it in GitHub Desktop.
code snippets for "MVP In Silverlight/WPF: Automated Tests" post
This file contains hidden or 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
[TestMethod] | |
public void SettingNameRaisesPropertyChangedEvent() | |
{ | |
AssertThatPropertyChangesIsTriggeredCorrectly(m => m.Name, "some name"); | |
} | |
[TestMethod] | |
public void SettingNameToInvalidValueCausesValidationError() | |
{ | |
BindingModel.Name = null; | |
AssertHasErrorMessageForProperty(m => m.Name, "name is a required field"); | |
} |
This file contains hidden or 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
[TestMethod] | |
public void Populate_SetsSelectedParentUserGroupIfCurrentGroupHasParent() | |
{ | |
var currentUserGroup = new UserGroupDto {Id = Guid.NewGuid(), ParentId = Guid.NewGuid()}; | |
var suitableParents = new[] {new UserGroupDto {Id = currentUserGroup.ParentId.Value}}; | |
BindingModel.Populate(suitableParents, currentUserGroup); | |
Assert.AreEqual(currentUserGroup.ParentId.Value, BindingModel.SelectedParentUserGroup.Id); | |
} | |
[TestMethod] | |
public void RevertToOriginalValues_SetsIdAndNameToOriginalValues() | |
{ | |
var userGroup = new UserGroupDto {Id = Guid.NewGuid(), Name = "some name"}; | |
BindingModel.Populate(new UserGroupDto[0], userGroup); | |
BindingModel.Id = Guid.NewGuid(); | |
BindingModel.Name = "some other name"; | |
BindingModel.RevertToOriginalValues(); | |
Assert.AreEqual(userGroup.Id, BindingModel.Id); | |
Assert.AreEqual(userGroup.Name, BindingModel.Name); | |
} |
This file contains hidden or 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
[TestMethod] | |
public void RetrievesUserGroupDetails() | |
{ | |
var userGroupId = Guid.NewGuid(); | |
Presenter.Handle(new UserGroupSelectedEvent(userGroupId)); | |
Assert.AreEqual(userGroupId, RequestDispatcherStub.GetRequest<GetUserGroupRequest>().UserGroupId); | |
} | |
[TestMethod] | |
public void RetrievesSuitableParentUserGroups() | |
{ | |
var userGroupId = Guid.NewGuid(); | |
Presenter.Handle(new UserGroupSelectedEvent(userGroupId)); | |
Assert.AreEqual(userGroupId, RequestDispatcherStub.GetRequest<GetSuitableParentUserGroupsRequest>().UserGroupId.Value); | |
} | |
[TestMethod] | |
public void ResponsesReceived_PopulatesModel() | |
{ | |
var userGroup = new UserGroupDto { Id = Guid.NewGuid() }; | |
var suitableParents = new[] { new UserGroupDto { Id = Guid.NewGuid() } }; | |
Presenter.Handle(new UserGroupSelectedEvent(Guid.NewGuid())); | |
RequestDispatcherStub.SetResponsesToReturn(new GetUserGroupResponse { UserGroup = userGroup }, | |
new GetSuitableParentUserGroupsResponse { SuitableParentUserGroups = suitableParents }); | |
RequestDispatcherStub.ReturnResponses(); | |
Assert.AreEqual(userGroup.Id, Presenter.BindingModel.Id); | |
Assert.AreEqual(suitableParents[0].Id, Presenter.BindingModel.SuitableParentUserGroups[1].Id); | |
} |
This file contains hidden or 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
[TestMethod] | |
public void ResponsesReceived_DoesNotTellViewToPreventModificationIfUserHasPermission() | |
{ | |
Presenter.Handle(new UserGroupSelectedEvent(Guid.NewGuid())); | |
RequestDispatcherStub.SetResponsesToReturn(new GetUserGroupResponse(), | |
new GetSuitableParentUserGroupsResponse {SuitableParentUserGroups = new UserGroupDto[0]}, | |
new CheckPermissionsResponse | |
{ | |
AuthorizationResults = new Dictionary<Guid, bool> {{Permissions.DeleteUserGroup, true}, {Permissions.EditUserGroup, true}} | |
}); | |
RequestDispatcherStub.ReturnResponses(); | |
ViewMock.Verify(v => v.PreventModification(), Times.Never()); | |
} | |
[TestMethod] | |
public void ResponsesReceived_ShowsTheView() | |
{ | |
var userGroup = new UserGroupDto { Id = Guid.NewGuid() }; | |
var suitableParents = new[] { new UserGroupDto { Id = Guid.NewGuid() } }; | |
Presenter.Handle(new UserGroupSelectedEvent(Guid.NewGuid())); | |
RequestDispatcherStub.SetResponsesToReturn(new GetUserGroupResponse { UserGroup = userGroup }, | |
new GetSuitableParentUserGroupsResponse { SuitableParentUserGroups = suitableParents }); | |
RequestDispatcherStub.ReturnResponses(); | |
ViewMock.Verify(v => v.Show()); | |
} |
This file contains hidden or 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
[TestMethod] | |
public void DoesNotProceedIfModelIsInvalid() | |
{ | |
Presenter.BindingModel.Name = null; | |
Presenter.PersistChanges(); | |
Assert.IsFalse(RequestDispatcherStub.HasRequest<SaveUserGroupRequest>()); | |
} |
This file contains hidden or 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
[TestMethod] | |
public void PublishesUserGroupDeletedEventWhenResponseIsReturned() | |
{ | |
Presenter.BindingModel.Id = Guid.NewGuid(); | |
RequestDispatcherStub.SetResponsesToReturn(new DeleteUserGroupResponse()); | |
Presenter.Delete(); | |
RequestDispatcherStub.ReturnResponses(); | |
Assert.AreEqual(Presenter.BindingModel.Id.Value, EventAggregatorStub.GetPublishedEvents<UserGroupDeletedEvent>()[0].UserGroupId); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment