In my project we are using
Rhino mocks for the automated unit testing. In that we are not able to
mock the HttpContext.Current.Cache
Please let me know if we can mock the cache for the unit testing using Rhinomocks
SOLUTION 1:
As per my information, to mock the HttpContext objects in Rhino Mocks, we have to stub the HttpContext.
PSB:
System under test:
01 public class
MyContext
02
{
03
private
readonly HttpContextBase
_context;
04
// This constructor is called by production system.
05
public
MyContext() : this(new HttpContextWrapper(HttpContext.Current))
06
{}
07
// Test calls this constructor
08
public
MyContext(HttpContextBase context)
09
{
10
_context = context;
11
}
12
public
bool IsItemCached(string
item)
13
{
14
return
_context.Cache[item] != null;
15
}
16
}
Unit tests:
01
[TestFixture]
02
public class
MyContextTester
03
{
04
private
HttpContextBase _contextStub;
05
private
MyContext _myContext;
06
[SetUp]
07
public
void StartTest()
08
{
09
_contextStub = MockRepository.GenerateMock<HttpContextBase>();
10
_myContext = new
MyContext(_contextStub);
11
}
12
[Test]
13
public
void IsItemCached_if_Cache_item_is_null_return_false()
14
{
15
_contextStub.Stub(x => x.Cache).Return(HttpRuntime.Cache);
16
Assert.IsFalse(_myContext.IsItemCached("NotThere"), "False is expected.");
17
}
18
[Test]
19
public
void IsItemCached_if_Cache_item_is_NOT_null_return_true()
20
{
21
HttpRuntime.Cache.Insert("I am HERE", "value");
22
_contextStub.Stub(x => x.Cache).Return(HttpRuntime.Cache);
23
Assert.IsTrue(_myContext.IsItemCached("I am HERE"), "True is expected.");
24
}
25
}
For your reference please refer: http://vkreynin.wordpress.com/tag/rhinomocks/
01 public class
MyContext
02
{
03
private
readonly HttpContextBase
_context;
04
// This constructor is called by production system.
05
public
MyContext() : this(new HttpContextWrapper(HttpContext.Current))
06
{}
07
// Test calls this constructor
08
public
MyContext(HttpContextBase context)
09
{
10
_context = context;
11
}
12
public
bool IsItemCached(string
item)
13
{
14
return
_context.Cache[item] != null;
15
}
16
}
01
[TestFixture]
02
public class
MyContextTester
03
{
04
private
HttpContextBase _contextStub;
05
private
MyContext _myContext;
06
[SetUp]
07
public
void StartTest()
08
{
09
_contextStub = MockRepository.GenerateMock<HttpContextBase>();
10
_myContext = new
MyContext(_contextStub);
11
}
12
[Test]
13
public
void IsItemCached_if_Cache_item_is_null_return_false()
14
{
15
_contextStub.Stub(x => x.Cache).Return(HttpRuntime.Cache);
16
Assert.IsFalse(_myContext.IsItemCached("NotThere"), "False is expected.");
17
}
18
[Test]
19
public
void IsItemCached_if_Cache_item_is_NOT_null_return_true()
20
{
21
HttpRuntime.Cache.Insert("I am HERE", "value");
22
_contextStub.Stub(x => x.Cache).Return(HttpRuntime.Cache);
23
Assert.IsTrue(_myContext.IsItemCached("I am HERE"), "True is expected.");
24
}
25
}
If you are interested in earning cash from your websites by running popunder ads, you should run one of the highest paying companies - Pop Cash.
ReplyDelete