Skip to content

Commit

Permalink
updated the unit tests - removed unwanted methods and used the constr…
Browse files Browse the repository at this point in the history
…uctor for setting custom properties in the framework objects
  • Loading branch information
karthik25 committed Jan 27, 2013
1 parent 89a8f77 commit 9c9445a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 54 deletions.
24 changes: 11 additions & 13 deletions sBlog.Net.Tests/Controllers/HomeControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class HomeControllerTests
public void Can_Return_First_Page_With_5_Items()
{
var postController = GetHomeControllerInstance();
postController.ControllerContext = new ControllerContext { HttpContext = GetHttpContext(false, 1) };
postController.ControllerContext = new ControllerContext { HttpContext = GetHttpContext(false, 0) };
var result = (ViewResult)postController.Index(null);
var posts = (result.ViewData.Model as BlogPostPageViewModel).Posts;
Assert.IsNotNull(posts);
Expand All @@ -28,7 +28,7 @@ public void Can_Return_First_Page_With_5_Items()
public void Can_Return_Second_Page_With_5_Items()
{
var postController = GetHomeControllerInstance();
postController.ControllerContext = new ControllerContext { HttpContext = new MockHttpContext() };
postController.ControllerContext = new ControllerContext { HttpContext = GetHttpContext(false, 0) };
var result = (ViewResult)postController.Index(2);
var posts = (result.ViewData.Model as BlogPostPageViewModel).Posts;
Assert.IsNotNull(posts);
Expand Down Expand Up @@ -76,7 +76,7 @@ public void Can_Return_Third_Page_With_5_Items_Authenticated()
public void Can_Return_Posts_By_Year_Month()
{
var postController = GetHomeControllerInstance();
postController.ControllerContext = new ControllerContext { HttpContext = GetHttpContext(false, 1) };
postController.ControllerContext = new ControllerContext { HttpContext = GetHttpContext(false, 0) };
var result = (ViewResult)postController.PostsByYearAndMonth("2012", "04", null);
var posts = (result.ViewData.Model as BlogPostPageViewModel).Posts;
Assert.IsNotNull(posts);
Expand All @@ -89,7 +89,7 @@ public void Can_Return_Posts_By_Year_Month()
public void Can_Return_Posts_By_Year_Month_Page_Two()
{
var postController = GetHomeControllerInstance();
postController.ControllerContext = new ControllerContext { HttpContext = GetHttpContext(false, 1) };
postController.ControllerContext = new ControllerContext { HttpContext = GetHttpContext(false, 0) };
var result = (ViewResult)postController.PostsByYearAndMonth("2012", "04", 2);
var posts = (result.ViewData.Model as BlogPostPageViewModel).Posts;
Assert.IsNotNull(posts);
Expand Down Expand Up @@ -128,7 +128,7 @@ public void Can_Return_Posts_By_Year_Month_Page_Two_Authenticated()
public void Can_Return_Posts_By_Tag_Name_Page_1()
{
var postController = GetHomeControllerInstance();
postController.ControllerContext = new ControllerContext { HttpContext = GetHttpContext(false, 1) };
postController.ControllerContext = new ControllerContext { HttpContext = GetHttpContext(false, 0) };
var result = (ViewResult)postController.PostsByTag("csharp", null);
var posts = (result.ViewData.Model as BlogPostPageViewModel).Posts;
Assert.IsNotNull(posts);
Expand All @@ -142,7 +142,7 @@ public void Can_Return_Posts_By_Tag_Name_Page_1()
public void Can_Return_Posts_By_Tag_Name_Page_2()
{
var postController = GetHomeControllerInstance();
postController.ControllerContext = new ControllerContext { HttpContext = GetHttpContext(false, 1) };
postController.ControllerContext = new ControllerContext { HttpContext = GetHttpContext(false, 0) };
var result = (ViewResult)postController.PostsByTag("csharp", 2);
var posts = (result.ViewData.Model as BlogPostPageViewModel).Posts;
Assert.IsNotNull(posts);
Expand Down Expand Up @@ -184,7 +184,7 @@ public void Can_Return_Posts_By_Tag_Name_Page_2_Authenticated()
public void Can_Return_Posts_By_Category_Name_Page_1()
{
var postController = GetHomeControllerInstance();
postController.ControllerContext = new ControllerContext { HttpContext = GetHttpContext(false, 1) };
postController.ControllerContext = new ControllerContext { HttpContext = GetHttpContext(false, 0) };
var result = (ViewResult)postController.PostsByCategory("CSharp", null);
var posts = (result.ViewData.Model as BlogPostPageViewModel).Posts;
Assert.IsNotNull(posts);
Expand All @@ -198,7 +198,7 @@ public void Can_Return_Posts_By_Category_Name_Page_1()
public void Can_Return_Posts_By_Category_Name_Page_2()
{
var postController = GetHomeControllerInstance();
postController.ControllerContext = new ControllerContext { HttpContext = GetHttpContext(false, 1) };
postController.ControllerContext = new ControllerContext { HttpContext = GetHttpContext(false, 0) };
var result = (ViewResult)postController.PostsByCategory("CSharp", 2);
var posts = (result.ViewData.Model as BlogPostPageViewModel).Posts;
Assert.IsNotNull(posts);
Expand Down Expand Up @@ -249,7 +249,7 @@ public void Can_Return_Posts_By_Category_Name_Page_3_Authenticated()
public void Can_Return_Posts_By_URL()
{
var postController = GetHomeControllerInstance();
postController.ControllerContext = new ControllerContext { HttpContext = GetHttpContext(false, 1) };
postController.ControllerContext = new ControllerContext { HttpContext = GetHttpContext(false, 0) };
var result = (ViewResult)postController.View("2012", "04", "a-test-url-1", "");
var post = (result.ViewData.Model as ViewPostOrPageModel).Post;
Assert.IsNotNull(post);
Expand Down Expand Up @@ -317,11 +317,9 @@ private static HomeController GetHomeControllerInstance()
return postController;
}

private MockHttpContext GetHttpContext(bool isAuthenticated, int userID)
private static MockHttpContext GetHttpContext(bool isAuthenticated, int userId)
{
var mockContext = new MockHttpContext();
mockContext.SetAuth(isAuthenticated);
mockContext.SetUserID(userID);
var mockContext = new MockHttpContext(userId, isAuthenticated);
return mockContext;
}
}
Expand Down
12 changes: 5 additions & 7 deletions sBlog.Net.Tests/Controllers/ViewPageControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ViewPageControllerTests
public void Can_Get_A_Single_Page()
{
var pageController = GetViewPageController();
pageController.ControllerContext = new ControllerContext { HttpContext = GetHttpContext(false, 1) };
pageController.ControllerContext = new ControllerContext { HttpContext = GetHttpContext(false, 0) };
var result = (ViewResult)pageController.Index("a-test-url-29", "");
var page = (result.ViewData.Model as ViewPostOrPageModel).Post;
Assert.IsNotNull(page);
Expand Down Expand Up @@ -53,7 +53,7 @@ public void Can_Throw_Exception_For_Illegal_Access()
public void Can_Generate_All_Pages()
{
var pageController = GetViewPageController();
pageController.ControllerContext = new ControllerContext { HttpContext = GetHttpContext(false, 1) };
pageController.ControllerContext = new ControllerContext { HttpContext = GetHttpContext(false, 0) };

var result = (PartialViewResult)pageController.Pages();
var blogMenuModel = result.ViewData.Model as BlogMenuViewModel;
Expand All @@ -73,7 +73,7 @@ public void Can_Generate_All_Pages()
public void Can_Generate_Other_Pages()
{
var pageController = GetViewPageController();
pageController.ControllerContext = new ControllerContext { HttpContext = GetHttpContext(false, 1) };
pageController.ControllerContext = new ControllerContext { HttpContext = GetHttpContext(false, 0) };

var result = (PartialViewResult)pageController.PagesList();
var posts = result.ViewData.Model as List<PostEntity>;
Expand Down Expand Up @@ -107,11 +107,9 @@ public void Can_Generate_Other_Pages_Authenticated()
Assert.AreEqual("a-test-url-40", second.PostUrl);
}

private MockHttpContext GetHttpContext(bool isAuthenticated, int userID)
private MockHttpContext GetHttpContext(bool isAuthenticated, int userId)
{
var mockContext = new MockHttpContext();
mockContext.SetAuth(isAuthenticated);
mockContext.SetUserID(userID);
var mockContext = new MockHttpContext(userId, isAuthenticated);
return mockContext;
}

Expand Down
30 changes: 12 additions & 18 deletions sBlog.Net.Tests/MockFrameworkObjects/MockHttpContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,31 @@ namespace sBlog.Net.Tests.MockFrameworkObjects
{
public class MockHttpContext : HttpContextBase
{
public MockHttpRequest httpRequest;
public MockHttpRequest HttpRequest;

private int _userID = 1;
private bool _isAuthenticated;
private readonly int _userId = 1;
private readonly bool _isAuthenticated;

public MockHttpContext(int userId, bool isAuthenticated)
{
_userId = userId;
_isAuthenticated = isAuthenticated;
}

public override HttpRequestBase Request
{
get
{
httpRequest = new MockHttpRequest();
httpRequest.SetAuth(_isAuthenticated);
return httpRequest;
HttpRequest = new MockHttpRequest(_isAuthenticated);
return HttpRequest;
}
}

public override IPrincipal User
{
get
{
var identity = new MockUserIdentity(null);
identity.SetUserID(_userID);
var identity = new MockUserIdentity(null, _userId);
IPrincipal principal = new GenericPrincipal(identity, null);
return principal;
}
Expand All @@ -35,15 +39,5 @@ public override IPrincipal User

}
}

public void SetUserID(int userID)
{
_userID = userID;
}

public void SetAuth(bool auth)
{
_isAuthenticated = auth;
}
}
}
12 changes: 6 additions & 6 deletions sBlog.Net.Tests/MockFrameworkObjects/MockHttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ namespace sBlog.Net.Tests.MockFrameworkObjects
{
public class MockHttpRequest : HttpRequestBase
{
private bool _isAuthenticated = false;
private readonly bool _isAuthenticated;

public MockHttpRequest(bool isAuthenticated)
{
_isAuthenticated = isAuthenticated;
}

public override Uri Url
{
Expand All @@ -22,10 +27,5 @@ public override bool IsAuthenticated
return _isAuthenticated;
}
}

public void SetAuth(bool isAuthenticated)
{
_isAuthenticated = isAuthenticated;
}
}
}
13 changes: 4 additions & 9 deletions sBlog.Net.Tests/MockObjects/MockUserIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ namespace sBlog.Net.Tests.MockObjects
public class MockUserIdentity : IIdentity, IUserInfo
{
private FormsAuthenticationTicket _ticket;
private readonly int _userId = 1;

private int _userID = 1;

public MockUserIdentity(FormsAuthenticationTicket ticket)
public MockUserIdentity(FormsAuthenticationTicket ticket, int userId)
{
_ticket = ticket;
_userId = userId;
}

public string AuthenticationType
Expand All @@ -32,17 +32,12 @@ public string Name

public string UserId
{
get { return _userID.ToString(); }
get { return _userId.ToString(); }
}

public string UserToken
{
get { return "TestToken"; }
}

public void SetUserID(int userID)
{
_userID = userID;
}
}
}
2 changes: 1 addition & 1 deletion sBlog.Net/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<configuration>
<connectionStrings>
<add name="AppDb" connectionString="Server=localhost;Database=sblog;user id=msuser;password=msuser;"/>
<add name="AppDb" connectionString="Server=localhost;Database=sblog;user id=msuser1;password=msuser1;"/>
</connectionStrings>

<appSettings>
Expand Down

0 comments on commit 9c9445a

Please sign in to comment.