Guest User

Untitled

a guest
May 22nd, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. Story: "Blog admin logs in to the system"
  2.  
  3. As a blog writer
  4. I want to be able to log in to my blog
  5. So that I can write posts and administrate my blog
  6.  
  7. Scenario: "Logs in from the login page"
  8.  
  9. Given the user enters in correct credentials for a user in the system
  10. When the user clicks the "Login" button
  11. Then log the user in and redirect to the admin panel with a message
  12. stating that he logged in correctly
  13.  
  14. using System;
  15. using Machine.Specifications;
  16. using Machine.Specifications.Model;
  17. using Moq;
  18. using MyBlog.Controllers;
  19. using System.Web.Mvc;
  20.  
  21. using MoqIt = Moq.It;
  22. using ThenIt = Machine.Specifications.It;
  23.  
  24.  
  25. [Subject("User tries logging in")]
  26. public class When_user_enters_valid_credentials : With_user_existing_in_membership
  27. {
  28. protected static ActionResult result;
  29.  
  30. Because of = () =>
  31. {
  32. result = loginController.Login(validUsername, validPassword);
  33. };
  34.  
  35. ThenIt should_log_the_user_in;
  36. ThenIt should_redirect_the_user_to_the_admin_panel;
  37. ThenIt should_show_message_confirming_successful_login;
  38. }
  39.  
  40. public abstract class With_user_existing_in_membership
  41. {
  42. protected static Mock<ISiteMembership> membershipMock;
  43. protected static string validUsername;
  44. protected static string validPassword;
  45. protected static LoginController loginController;
  46.  
  47. Establish context =()=>
  48. {
  49. membershipMock = new Mock<ISiteMembership>();
  50. validUsername = "ValidUsername";
  51. validPassword = "ValidPassword";
  52. //make sure it's treated as valid usernames and password
  53. membershipMock.Setup<bool>(m => m.Validate(MoqIt.Is<string>(s => s == validUsername), MoqIt.Is<string>(s => s == validPassword)))
  54. .Returns(true);
  55. loginController = new LoginController(membershipMock.Object);
  56. };
  57.  
  58. }
  59.  
  60. // The "Subject" attribute is used as the "Scenario" in
  61. // in your original story, and gets rendered with your
  62. // MSpec report that is outputted (especially the HTML version).
  63. [Subject("Login Page")]
  64. public class When_user_enters_valid_credentials_for_existing_user
  65. : MembershipContext
  66. {
  67. static ActionResult result;
  68. static string username;
  69. static string password;
  70.  
  71. // This is optional, but will inherit the MembershipContext, and
  72. // allow for more specific setup of your context for these
  73. // grouped specs only.
  74. Establish context = () =>
  75. {
  76. username = "ValidUsername";
  77. password = "ValidPassword";
  78.  
  79. // This is context-specific: Username/password is valid
  80. membership.Setup<bool>(
  81. m => m.Validate(
  82. Param.Is<string>(s => s == username)
  83. ,Param.Is<string>(s => s == password)
  84. )
  85. ).Returns(true);
  86. };
  87.  
  88. Because of = () =>
  89. {
  90. result = loginController.Login(username, password);
  91. };
  92.  
  93. It should_log_the_user_in;
  94. It should_redirect_the_user_to_the_admin_panel;
  95. It should_show_message_confirming_successful_login;
  96. }
  97.  
  98. public abstract class MembershipContext
  99. {
  100. protected static Mock<ISiteMembership> membership;
  101. protected static LoginController loginController;
  102.  
  103. Establish context = () =>
  104. {
  105. membership = new Mock<ISiteMembership>();
  106. loginController = new LoginController(membershipMock.Object);
  107. };
  108. }
Add Comment
Please, Sign In to add comment