Furthermore if you wanted to mock something like
HttpContext.Current.Server.MapPath(.NET Classes/Types) you would have to
wrap it in a class and make the member that does this work virtual ..
public interface IHttpContextService
{
string MapPath(string webUrl);
}
public class HttpContextService : IHttpContextService
{
public string MapPath(string webUrl)
{
return HttpContext.Current.Server.MapPath(webUrl);
}
}
public class ThingWhatUsesMapPath
{
private readonly IHttpContextService contextService;
public ThingWhatUsesMapPath(IHttpContextService contextService)
{
this.contextService = contextService;
}
public string ThingWhatDoesStuff(string mapIt)
{
return contextService.MapPath(mapIt);
}
}
[TestFixture]
public class ThingWhatUsesMapPathTests
{
[Test]
public void ThingWhatDoesStuff_should_MapPath()
{
var fakeContextService = MockRepository.GenerateStub<
IHttpContextService>();
var objectUnderTest = new ThingWhatUsesMapPath
(fakeContextService);
objectUnderTest.ThingWhatDoesStuff("AnyPath");
fakeContextService.AssertWasCalled(x => x.MapPath(Arg<string
>.Is.Equal("AnyPath")));
}
}
On 10 November 2011 23:33, Stephen Bohlen <[email protected]> wrote:
> Only if the methods in the concrete class are declared virtual. The two
> kinds of things that can be mocked with RhinoMocks are:
>
> 1) interfaces
> 2) virtual methods in unsealed classes
>
> Steve Bohlen
> [email protected]
> http://blog.unhandled-exceptions.com
> http://twitter.com/sbohlen
>
>
>
> On Thu, Nov 10, 2011 at 6:10 PM, Laksh <[email protected]> wrote:
>
>> correct..but not all the classes in .Net or my custom classes are
>> sealed. You are saying if the class is not sealed, i can mock it
>> without having the interface?
>>
>> On Nov 10, 3:27 pm, Patrick Steele <[email protected]> wrote:
>> > Mocking an interface is easier. You can mock the virtual members of a
>> > non-sealed class. If a class is sealed or the members you want to
>> > mock are not virtual, you'll have to create a wrapper (possibly in the
>> > form of an interface) in order to do your mocking.
>> >
>> > ---
>> > Patrick Steelehttp://weblogs.asp.net/psteele
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> > On Thu, Nov 10, 2011 at 3:07 PM, Laksh <[email protected]> wrote:
>> > > Thanks.
>> > > I'm new rhino mocks and never used it before..so the questions.
>> > > One last question. Is it necessary to derive all the classes from some
>> > > kind of interface? How do we mock .Net classes?
>> > > for example
>> > > MockRepository mocks = new MockRepository();
>> > > var myClass = mocks.Stub<MyClass>();
>> > > var list = mocks.DynamicMock<Array>();
>> >
>> > > On Nov 10, 11:51 am, Patrick Steele <[email protected]> wrote:
>> > >> Correct. Mocking only works if you control creation of the object.
>> >
>> > >> ---
>> > >> Patrick Steelehttp://weblogs.asp.net/psteele
>> >
>> > >> On Thu, Nov 10, 2011 at 12:41 PM, Gavin van der Merwe
>> >
>> > >> <[email protected]> wrote:
>> > >> > I think Patrick is saying that mocking MyClass this way is not
>> possible.
>> > >> > You need to structure your class in a de-coupled way ...
>> > >> > public class ClassThatDoesWork
>> > >> > {
>> > >> > private MyClass instance;
>> > >> > public ClassThatDoesWork(MyClass instance)
>> > >> > {
>> > >> > this.instance = instance;
>> > >> > }
>> > >> > public void DoWork(someInputParameters)
>> > >> > {
>> > >> > // do stuff with mocked instance
>> > >> > }
>> > >> > }
>> >
>> > >> > On 10 November 2011 17:31, Laksh <[email protected]> wrote:
>> >
>> > >> >> Thanks, i'll would do that, and we are using Repository pattern,
>> so
>> > >> >> its easy change.
>> >
>> > >> >> i have a question though. In my scenario the method returns the
>> > >> >> Package class so the caller of this method knows Package class.so
>> i'm
>> > >> >> able to Assert the values.
>> > >> >> If i have a method which internally creates instance of class,
>> would
>> > >> >> it be possible to mock that class. see the example below
>> >
>> > >> >> public void DoWork(someInputParameters)
>> > >> >> {
>> > >> >> MyClass instance = new MyClass()
>> > >> >> // do some logic here.
>> > >> >> // I may use some .Net classes also to perform the logic
>> > >> >> }
>> >
>> > >> >> is it possible to mock MyClass & .Net classes here ?
>> >
>> > >> >> On Nov 10, 10:18 am, Patrick Steele <[email protected]>
>> wrote:
>> > >> >> > You want to make sure you have a loosely coupled architecture
>> so you
>> > >> >> > can do your unit tests in isolation.
>> >
>> > >> >> > The Manifest class you originally showed looks like it needs to
>> get
>> > >> >> > data from a database (The GetData method). That dependency on a
>> > >> >> > database should be in a different component. One idea is to
>> create a
>> > >> >> > repository interface for accessing the data:
>> >
>> > >> >> > interface IDataObjectRepository
>> > >> >> > {
>> > >> >> > DataObject GetData(IContext);
>> >
>> > >> >> > }
>> >
>> > >> >> > Your Manifest class now has a dependency on the
>> IDataObjectRepository
>> > >> >> > which can be mocked at test time using Rhino.Mocks. This way
>> you
>> > >> >> > don't have to have an actual database up and running to execute
>> your
>> > >> >> > unit test for BuildManifest(). You don't have to worry about
>> proper
>> > >> >> > table structures and things like that.
>> >
>> > >> >> > So iyour Manifest class now takes in the repository:
>> >
>> > >> >> > public class Manifest : IManifest<Package>
>> > >> >> > {
>> > >> >> > private IContext _context = null;
>> > >> >> > private IDataObjectReposutory _repository = null;
>> >
>> > >> >> > public Manifest(IContext context, IDataObjectReposutory
>> > >> >> > repository)
>> > >> >> > {
>> > >> >> > _context = context;
>> > >> >> > _repository = repository;
>> > >> >> > }
>> >
>> > >> >> > public Package BuildManifest()
>> > >> >> > {
>> > >> >> > ...
>> > >> >> > DataObject data = _repository.GetData(_context);
>> > >> >> > ...
>> > >> >> > }
>> >
>> > >> >> > }
>> >
>> > >> >> > Now you can mock your context and repository and test to make
>> sure
>> > >> >> > that your Package gets built properly based on the data
>> returned by
>> > >> >> > IDataObjectRepository.GetData():
>> >
>> > >> >> > // arrange
>> > >> >> > var context = MockRepository.GenerateStub<IContext>();
>> > >> >> > var repository =
>> MockRepository.GenerateStub<IDataObjectRespository>();
>> > >> >> > var do = new new DataObject { BatchID = "100", Title="Test" };
>> > >> >> > repository.Stub(r => r.GetData(context)).Returns(do);
>> >
>> > >> >> > // act
>> > >> >> > var manifest = new Manifest(context, repository);
>> > >> >> > var pacakgeManifest = manifest.BuildManifest();
>> >
>> > >> >> > // assert
>> > >> >> > Assert.AreEqual(do.BatchID, packageManifest.BatchID);
>> > >> >> > Assert.AreEqual(do.Title, packageManifest.Title);
>> > >> >> > etc...
>> >
>> > >> >> > ---
>> > >> >> > Patrick Steelehttp://weblogs.asp.net/psteele
>> >
>> > >> >> > On Thu, Nov 10, 2011 at 10:39 AM, Laksh <[email protected]>
>> wrote:
>> > >> >> > > GetData method gets the data from the database and then I
>> assign the
>> > >> >> > > data to Package class hierarchy.
>> > >> >> > > I'm creating the instance of Package inside the method. So
>> how Rhino
>> > >> >> > > will test such things. My question was not only for this
>> method. In
>> > >> >> > > general it is very common we create instances of custom
>> classes, .Net
>> > >> >> > > classes inside the method. (Basically local variables). In
>> such case
>> > >> >> > > how do test the method using Rhino?
>> >
>> > >> >> > > On Nov 9, 10:33 am, Patrick Steele <[email protected]>
>> wrote:
>> > >> >> > >> What does GetData do? You probably need to stub that out,
>> but
>> > >> >> > >> without
>> > >> >> > >> knowing what it does, I can't say for sure.
>> >
>> > >> >> > >> ---
>> > >> >> > >> Patrick Steelehttp://weblogs.asp.net/psteele
>> >
>> > >> >> > >> On Wed, Nov 9, 2011 at 11:20 AM, Laksh <[email protected]>
>> wrote:
>> > >> >> > >> > I have the following method. I'm creating a instance of
>> Package
>> > >> >> > >> > class
>> > >> >> > >> > inside the method, setting some of its proeprties and then
>> > >> >> > >> > returning
>> > >> >> > >> > it. How do i unit test this method using Rhino?
>> >
>> > >> >> > >> > public class Manifest : IManifest<Package>
>> > >> >> > >> > {
>> > >> >> > >> > private IContext _context = null;
>> >
>> > >> >> > >> > publicManifest(IContext context)
>> > >> >> > >> > {
>> >
>> > >> >> > >> > _context = context;
>> > >> >> > >> > }
>> >
>> > >> >> > >> > public Package BuildManifest()
>> > >> >> > >> > {
>> > >> >> > >> > // Data object is class which holds the data
>> from
>> > >> >> > >> > datasource.
>> > >> >> > >> > DataObject data = GetData(_context);
>> >
>> > >> >> > >> > Package package = new Package();
>> > >> >> > >> > package.BatchID = data.BatchId;
>> > >> >> > >> > package.Title = data.Title;
>> > >> >> > >> > package.Name = data.Name;
>> > >> >> > >> > package.City = data.City;
>> >
>> > >> >> > >> > IList<PackageDocument> documents = new
>> > >> >> > >> > List<PackageDocument>();
>> > >> >> > >> > foreach (Document document in
>> data.ConvertedDocuments)
>> > >> >> > >> > {
>> > >> >> > >> > PackageDocument packageDocument = new
>> > >> >> > >> > PackageDocument();
>> > >> >> > >> > packageDocument.FileName =
>> document.DocumentName;
>> > >> >> > >> > packageDocument.PrintOrder =
>> > >> >> > >> > document.SequenceNumber.Value;
>> > >> >> > >> > packageDocument.PaperType =
>> > >> >> > >> > PackageDocumentPaperType.White;
>> > >> >> > >> > documents.Add(packageDocument);
>> > >> >> > >> > }
>> > >> >> > >> > package.Documents = documents.ToArray();
>> > >> >> > >> > return package;
>> > >> >> > >> > }
>> > >> >> > >> > }
>> >
>> > >> >> > >> > --
>> > >> >> > >> > You received this message because you are subscribed to
>> the Google
>> > >> >> > >> > Groups "Rhino.Mocks" group.
>> > >> >> > >> > To post to this group, send email to
>> [email protected].
>> > >> >> > >> > To unsubscribe from this group, send email to
>> > >> >> > >> > [email protected].
>> > >> >> > >> > For more options, visit this group
>> > >> >> > >> > athttp://groups.google.com/group/rhinomocks?hl=en.
>> >
>> > >> >> > > --
>> > >> >> > > You received this message because you are subscribed to the
>> Google
>> > >> >> > > Groups "Rhino.Mocks" group.
>> > >> >> > > To post to this group, send email to
>> [email protected].
>> > >> >> > > To unsubscribe from this group, send email to
>> > >> >> > > [email protected].
>> > >> >> > > For more options, visit this group
>> > >> >> > > athttp://groups.google.com/group/rhinomocks?hl=en.
>> >
>> > >> >> --
>> > >> >> You received this message because you are subscribed to the
>> Google Groups
>> > >> >> "Rhino.Mocks" group.
>> > >> >> To post to this group, send email to [email protected].
>> > >> >> To unsubscribe from this group, send email to
>> > >> >> [email protected].
>> > >> >> For more options, visit this group at
>> > >> >>http://groups.google.com/group/rhinomocks?hl=en.
>> >
>> > >> > --
>> > >> > You received this message because you are subscribed to the Google
>> Groups
>> > >> > "Rhino.Mocks" group.
>> > >> > To post to this group, send email to [email protected].
>> > >> > To unsubscribe from this group, send email to
>> > >> > [email protected].
>> > >> > For more options, visit this group at
>> > >> >http://groups.google.com/group/rhinomocks?hl=en.
>> >
>> > > --
>> > > You received this message because you are subscribed to the Google
>> Groups "Rhino.Mocks" group.
>> > > To post to this group, send email to [email protected].
>> > > To unsubscribe from this group, send email to
>> [email protected].
>> > > For more options, visit this group athttp://
>> groups.google.com/group/rhinomocks?hl=en.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Rhino.Mocks" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/rhinomocks?hl=en.
>>
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Rhino.Mocks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/rhinomocks?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"Rhino.Mocks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rhinomocks?hl=en.