How can I mock Service.SaveSession(user)?

25 views
Skip to first unread message

Louis Ferreira

unread,
Feb 19, 2015, 9:28:47 AM2/19/15
to servic...@googlegroups.com
I am using Moq and want to unit test a class in my service. One of the methods in my class-under-test takes a ServiceStack.Service object param, and in the methods' body, I am calling service.SaveSession(user). I can easily mock out the Service object, but the SaveSession() method is an extension method that moq cannot mock.
Is there another non static non extension method that does the same thing as SaveSession() that I can refactor my code to use in order to make it more testable?

here is a simplified version of what I am trying to achieve...

public class MyManager {
public void DoSomething(Service service) {
var user = service.GetSession();   //this is mockable
// do something with user
service.SaveSession(user);  //this is not mockable! REFACTOR ?
}
}

using Xunit;
using Moq;
public class MyManagerTests {
private Mock<Service> _mockService;
private MyManager _classUnderTest;
 
public MyManagerTests() {
_mockService = new Mock<Service>();
_classUnderTest = new MyManager(); 
_mockService.Setup(x => x.GetSession(It.IsAny<bool>())).Returns(new AuthUserSession());
}
 
[Fact]
public void Test_DoSomething() {
_classUnderTest.DoSomething(_mockService.Object);
}

}

Thanks
Reply all
Reply to author
Forward
0 new messages