How can i test a method which clear the session and logoff user.
my controller method looks like
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
SessionAdapter.Clear();
SessionAdapter.Abandon();
AuthenticationManager.SignOut
(DefaultAuthenticationTypes.ApplicationCookie);
return RedirectToAction("Index", "Home");
}
Here session adapter is my static class
public static class SessionAdapter
{
private static string sessionKey = "sessionInfoKey";
public static SessionInfo Instance
{
get
{
return HttpContext.Current.Session[sessionKey] == null ? null : (SessionInfo)HttpContext.Current.Session[sessionKey];
}
set
{
HttpContext.Current.Session[sessionKey] = value;
}
}
public static bool DoesSessionExists { get { return HttpContext.Current.Session[sessionKey] == null ? false : true; } }
public static void Clear()
{
HttpContext.Current.Session.Clear();
}
}
Please help me
In a unit test you should mocking out session since it's an external dependency. Your unit tests should be testing your code, not the .net framework.
So a valid test might be to verify that a call to Session.Clear happened, not actually testing Session.Clear clears out a session.
This can be done by setting up a fake session. Here's a extension method I use to setup my controller context for unit testing
public static void SetControllerContext(this Controller controller)
{
var fakeContext = A.Fake();
var fakeRequest = A.Fake();
var fakeResponse = A.Fake();
var fakeSessionState = A.Fake();
A.CallTo(() => fakeRequest.HttpMethod).Returns(HttpVerbs.Post.ToString());
A.CallTo(() => fakeContext.Response).Returns(fakeResponse);
A.CallTo(() => fakeContext.Request).Returns(fakeRequest);
A.CallTo(() => fakeContext.Session).Returns(fakeSessionState);
var fakeRequestContext = new RequestContext(fakeContext, new RouteData());
controller.ControllerContext = new ControllerContext(fakeRequestContext, controller);
}
This is using FakeItEasy, but the same thing can be done with Moq.
From MS "ASP.NET session state enables you to store and retrieve values for a user as the user navigates ASP.NET pages in a Web application."
What's the value in wrapping that in a static class?
Related
I am just getting started with unit testing and now stuck writing a test method for delete. I am using JustMock. Below I have mentioned my controller action method and test method but the test method is not working. The return value of the method is not retaining it's always returning false. Please guide me on what I'm doing wrong here.
This is the test method.
[TestMethod]
public void Delete_User()
{
Mapper.CreateMap<UserViewModel,User>();
IStaticMembershipService membershipRepository = Mock.Create<IStaticMembershipService>();
var mockUserViewModel = new UserViewModel { Id = 1};
var mockUser = Mapper.Map<UserViewModel, User>(mockUserViewModel);
var UserRepository = Mock.Create<IUserRepository>();
Mock.Arrange(() => UserRepository.DeleteUser(mockUser)).Returns(new OperationStatus { Status = true }).MustBeCalled();
UserController controller = new UserController(UserRepository, membershipRepository);
ActionResult result = controller.Delete(mockUserViewModel);
Mock.Assert(() => UserRepository.DeleteUser(mockUser), Occurs.AtLeastOnce());
}
Any relevant controller info.
private IUserRepository _UserRepository;
private IStaticMembershipService _membershipService;
public UserController(IUserRepository UserRepo, IStaticMembershipService membershipService)
{
_UserRepository = UserRepo;
_membershipService = membershipService;
}
The repository is mocked correctly but the "opStatus" property getting returned is always false not the value I have specified to be the result in testmethod.
public ActionResult Delete(UserViewModel usr)
{
var selectedUser = Mapper.Map<UserViewModel, User>(usr);
var opStatus = _UserRepository.DeleteUser(selectedUser);
if (!opStatus.Status)
throw new System.Web.Http.HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
return RedirectToAction("Index");
}
I hope i have provided enough information. Any help much appreciated.
Change:
Mock.Arrange(() => UserRepository.DeleteUser(mockUser)).Returns(new OperationStatus { Status = true }).MustBeCalled();
to:
Mock.Arrange(() => UserRepository.DeleteUser(mockUser)).IgnoreArguments().Returns(new OperationStatus { Status = true }).MustBeCalled();
Alternatively, add equality operators to User so that it realises that mockUser and selectedUser are the 'same'. Or use lambdas for argument matching (see http://www.telerik.com/blogs/handling-arguments-in-justmock-arrangements).
I want to test WebMethod of some Web Service (asmx). Suppose I have the following code:
public IUsersRepository UsersRepository
{
get { return Session["repository"] as IUsersRepository; }
set { Session["repository"] = value; }
}
[WebMethod(EnableSession = true)]
public int AddUser(string userName, int something)
{
var usersRepository = Session["repository"] as IUsersRepository;
return usersRepository.AddUser(userName, something);
}
and the corresponding unit test (just to test that the repository is called at all):
[Test]
public void add_user_adds_user()
{
// Arrange
var repository = new Mock<IUsersRepository>();
var service = new ProteinTrackingService { UsersRepository = repository.Object };
// Act
var userName = "Tester";
var something = 42;
service.AddUser(userName: userName, something: something);
// Assert
repository.Verify(r => r.AddUser(
It.Is<string>(n => n.Equals(userName)),
It.Is<int>(p => p.Equals(something))));
}
When I run this test, I receive the following error message:
System.InvalidOperationException : HttpContext is not available.
This class can only be used in the context of an ASP.NET request.
What shall I do to make this test working?
Have you had a look at this one? Setting HttpContext.Current.Session in a unit test Apparently should can do that trick to simulate your session.
On regards to your assert, you can directly do:
// Assert
repository.Verify(r => r.AddUser(userName, something));
And that will assert you are calling that method with these parameters.
Hope this helps!
I am presently working on unit testing using Microsoft Fakes and testing a controller which uses some session variables.
Since no session is being initiated during unit test creation whenever I'm running unit test I'm stuck with a NullReferenceException.I have seen many question and answers doing it using Moqs but I want it in Microsoft Fakes.
I know I need to use shims to fake session variable ,since I'm not having a clear idea about how exactly session is being created I'm stuck there.
Please explain me how exactly a session is created so that I can fake it,also if possible please let me know how to write it in microsoft fakes
Well, you can this as example :
public class SomeClass
{
public bool SomeMethod()
{
var session = HttpContext.Current.Session;
if (session["someSessionData"].ToString() == "OK")
return true;
return false;
}
}
[TestMethod]
public void SomeTestMethod()
{
using (ShimsContext.Create())
{
var instanceToTest = new SomeClass();
var session = new System.Web.SessionState.Fakes.ShimHttpSessionState();
session.ItemGetString = (key) => { if (key == "someSessionData") return "OK"; return null; };
var context = new System.Web.Fakes.ShimHttpContext();
System.Web.Fakes.ShimHttpContext.CurrentGet = () => { return context; };
System.Web.Fakes.ShimHttpContext.AllInstances.SessionGet =
(o) =>
{
return session;
};
var result = instanceToTest.SomeMethod();
Assert.IsTrue(result);
}
}
Please see http://blog.christopheargento.net/2013/02/02/testing-untestable-code-thanks-to-ms-fakes/ for more details.
Have a nice day.
I have a controller called PostsController
public class PostsController : Controller
{
private const int PageSize = 8;
private readonly IPostsRepository repository;
public PostsController(IPostsRepository repository)
{
this.repository = repository;
}
public ViewResult Index(int pageNumber = 1)
{
var posts =
repository.All()
.Where(post => !post.Draft)
.OrderBy(post => post.PublishedAt);
var model =
posts.MapTo<PostViewModel>()
.ToPagedList(pageNumber, PageSize);
return View("Index", model);
}
public ActionResult Post(string slug)
{
var post =
repository.Find(slug);
if (post == null || post.Draft)
{
return HttpNotFound();
}
return View("Post", post.MapTo<PostViewModel>());
}
}
And a corresponding test fixture called PostsControllerTest
[TestFixture]
public class PostsControllerTest
{
private PostsController controller;
private Mock<IPostsRepository> repository;
[SetUp]
public void SetUp()
{
AutoMapperConfig.Configure();
repository = new Mock<IPostsRepository>();
controller = new PostsController(repository.Object);
}
[Test]
public void Index_ReturnsCorrectViewName()
{
var actual = controller.Index();
Assert.AreEqual(actual.ViewName, "Index");
}
[Test]
public void Index_ReturnsCorrectModel()
{
var result = controller.Index();
var actual = result.Model as PagedList<PostViewModel>;
Assert.NotNull(actual);
}
[Test]
public void Index_WithPageNumber_ReturnsCorrectViewName()
{
var actual = controller.Index(2);
Assert.AreEqual(actual.ViewName, "Index");
}
[Test]
public void Index_WithPageNumber_ReturnsCorrectModel()
{
var result = controller.Index(2);
var actual = result.Model as PagedList<PostViewModel>;
Assert.NotNull(actual);
}
[Test]
public void Post_ReturnsCorrectViewName()
{
repository.Setup(repo => repo.Find("abc"))
.Returns(new Post());
var actual = controller.Post("abc") as ViewResult;
Assert.NotNull(actual);
Assert.AreEqual(actual.ViewName, "Post");
}
[Test]
public void Post_ThatIsDraft_ReturnsNotFound()
{
var post = new Post { Draft = true };
repository.Setup(repo => repo.Find("abc"))
.Returns(post);
var actual = controller.Post("abc");
Assert.IsAssignableFrom<HttpNotFoundResult>(actual);
}
[Test]
public void Post_ThatDoesNotExist_ReturnNotFound()
{
var actual = controller.Post("abc");
Assert.IsAssignableFrom<HttpNotFoundResult>(actual);
}
[Test]
public void Post_ReturnsCorrectModel()
{
var post = new Post
{
Slug = "continuing-to-an-outer-loop",
Title = "Continuing to an outer loop",
Summary = "When you have a nested loop, sometimes",
Content = "When you have a nested loop, sometimes",
PublishedAt = DateTime.Now.AddDays(7),
Tags = new Collection<Tag> { new Tag { Name = "Programming" } }
};
repository.Setup(repo => repo.Find("continuing-to-an-outer-loop"))
.Returns(post);
var viewResult = (ViewResult)controller.Post("continuing-to-an-outer-loop");
var actual = viewResult.Model as PostViewModel;
Assert.NotNull(actual);
Assert.AreEqual(actual.Slug, post.Slug);
Assert.AreEqual(actual.Title, post.Title);
Assert.AreEqual(actual.Summary, post.Summary);
Assert.AreEqual(actual.Content, post.Content);
Assert.AreEqual(actual.PublishedAt, post.PublishedAt);
Assert.AreEqual(actual.Tags, post.Tags);
}
}
I learned to mock the repository in this way by observing how other projects arranged their tests. One particular example of this approach (from which I learned) can be found here. Personally I have found this approach to be somewhat laborious. Furthermore, since adopting this approach I stumbled upon this post that states that you should not mock your repository but fake it instead.
Now I am conflicted. Should I proceed to mock my repository of fake it instead?
I kindly ask that you include code examples that show how to fake and seed the repository in this case as I am not sure how to do so.
To pick a side, I'd say mocking is just fine and is less work than faking.
But to voice an opinion - without trying to be awkward - I'd say... neither.
Consider how much value is being added by a test which goes through your Index action without hitting a real repository - the vast majority of the code you're testing is in Linq and AutoMapper, both of which have already been heavily tested by other people.
I would recommended writing Integration tests which run through your controller actions, through your real repositories, hit your database and come back out the other side. That adds value and gives you some real assurance that your system (i.e. the bits you've written) actually works.
Finally, Jimmy Bogard (of AutoMapper fame) has a good blog entry about testing repositories here.
I am testing the create class in Visual Studio 2012
My controller class is:
public ActionResult Create()
{
return View();
}
//
// POST: /Member/Create
[HttpPost]
public ActionResult Create(Member member)
{
if (ModelState.IsValid)
{
db.Members.Add(member);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(member);
}
And test class is:
[TestClass]
public class MemberTest
{
[TestMethod]
public void Create(Member mem)
{
mem.MemID = 123;
mem.MemName = "sruthy";
/// dont know what is writing.
}
}
SampleDataContext.cs
public class SampleDataContext:DbContext
{
public DbSet<Member> Members { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
I am stuck in test case please help me.
First - create an abstraction for your data access code (mocking DbContext is not very convenient thing):
public interface IMemberRepository
{
void Add(Member member);
}
and make your controller depend on it
public MemberController(IMemberRepository repository)
{
this.repository = repository;
}
This will allow mock data access code easily. Next - write tests which verify controller behavior (I use NUnit and Moq here):
private MemberController controller;
private Mock<IMemberRepository> repositoryMock;
private Member member;
[SetUp]
public void Setup()
{
repositoryMock = new Mock<IMemberRepository>();
controller = new MemberController(repositoryMock.Object);
member = new Member { MemID = 123, MemName = "sruthy" };
}
[Test]
public void ShouldCreateMemberWhenItIsValid()
{
var result = (RedirectToRouteResult)controller.Create(member);
Assert.That(result.RouteValues["action"], Is.EqualTo("Index"));
repositoryMock.Verify(r => r.Add(member));
}
[Test]
public void ShouldNotCreateMemberWhenItIsNotValid()
{
controller.ModelState.AddModelError("MemName", "Something wrong");
var result = (ViewResult)controller.Create(member);
Assert.That(result.ViewName, Is.Empty);
}
And write implementation:
[HttpPost]
public ActionResult Create(Member member)
{
if (ModelState.IsValid)
{
repository.Add(member);
return RedirectToAction("Index");
}
return View(member);
}
What I understood in unit testing is : "test only what your method is doing" So I think you have to test your method is doing well:
ModelState.IsValid
db.Members.Add(member)
db.SaveChanges()
But not the good behavior of ModelState or DbContext. These are tested in their own unit tests. You have to assert only the call is done.
To perform this kind of test you have to use the Dependency injection pattern and replace the real DbContext by mocks. These mocks are just asserting the call is well executed without involving the real dbContext.
I'm not a specialist in unit testing but I think you have to think all your architecture in order to decouple your objects. This allow you to replace real objects by mocks.