Ok, I declared this in main project:
public interface IFileSystem
{
bool FileExists(string filePath);
}
I've changed implementation on ClassA to following:
public class ClassA:baseClass, IFileSystem
{
public bool FileExists(string filePath)
{
return File.Exists(filePath);
}
...
Now, I would still need to access protected method through
ClassA_AccessorManualClass, as such I've changed the test to
following:
[TestInitialize()]
public void MyTestInitialize() {
_mocks = new MockRepository();
_classManualAccessorMock =
_mocks.PartialMultiMock<ClassA_AccessorManualClass>(typeof
(IFileSystem));
}
//
// Use TestCleanup to run code after each test has run
[TestCleanup()]
public void MyTestCleanup() {
_mocks.VerifyAll();
}
//
#endregion
[TestMethod]
public void TestProcessItem()
{
IFileSystem fs = _classManualAccessorMock;
Expect.Call(fs.FileExists(null)).IgnoreArguments().Return
(true);
_mocks.ReplayAll();
_classManualAccessorMock.ProcessItem(1);
Assert.AreEqual(true, _classManualAccessorMock.Success);
Assert.AreEqual("test 1.test",
_classManualAccessorMock.FileName);
}
And it is passing now!
Thank you everyone.
On Mar 24, 4:45 pm, Tim Barcz <[email protected]> wrote:
> You can then mock IFileSystem.FileExists to return true or false, allowing
> you to
>
> 1. Not rely on the physical file system where the tests are running
> 2. Tests both paths through this method (1) the file exists 2) the file
> doesn't exist and make sure the behavior you want to happen IS in fact
> happening.
>
> Tim
>
> On Tue, Mar 24, 2009 at 3:43 PM, Jake <[email protected]> wrote:
>
> > What would I gain by astracting away the interface? If need test to
> > execute something before the call to FileExists and after the call to
> > FileExists then how having interface changes anything?
>
> > On Mar 24, 4:37 pm, Chris Martin <[email protected]> wrote:
> > > Maybe to abstract it away as IFileSystem?
> > > Unfortunately, I really couldn't tell you the best way to do such a thing
> > > without knowing your project.
>
> > > On Tue, Mar 24, 2009 at 1:35 PM, Jake <[email protected]> wrote:
>
> > > > Ok, I understand that having FileExists as separate method is "code
> > > > smell", but regardless of that if I need to test functionality that is
> > > > depended on File.Exists (FileInfo.Exists is another alternative I've
> > > > tried to use) how can you test that?
>
> > > > On Mar 24, 4:31 pm, Chris Martin <[email protected]> wrote:
> > > > > You really can't unless you completely mock every call in the chain.
> > > > Which
> > > > > would invalidate any test.
> > > > > I'd take this as a sign that your design isn't sound. :(
>
> > > > > On Tue, Mar 24, 2009 at 12:57 PM, Jake <[email protected]> wrote:
>
> > > > > > So how can I mock that underlying object to test the functionality
> > in
> > > > > > ProcessItem?
>
> > > > > > On Mar 24, 3:39 pm, Chris Martin <[email protected]> wrote:
> > > > > > > Looking under the hood, this is what I can determine:
> > > > > > > MSTest is generating a proxy to the real object under test. It's
> > only
> > > > > > doing
> > > > > > > this to be able to call the real object. Guess what? The real
> > object
> > > > in
> > > > > > turn
> > > > > > > calls FileExists, which isn't mocked. The result you're seing is
> > the
> > > > > > result
> > > > > > > of File.Exists in Sys.IO.
>
> > > > > > > On Tue, Mar 24, 2009 at 12:33 PM, Jake <[email protected]>
> > wrote:
>
> > > > > > > > ClassA_Accessor gets generated on runtime and makes otherwise
> > > > "hidden"
> > > > > > > > methods and members available for testing. And Yes it is used
> > by
> > > > > > > > MSTest only.
>
> > > > > > > > On Mar 24, 3:31 pm, Chris Martin <[email protected]> wrote:
> > > > > > > > > What is ClassA_Accessor?
> > > > > > > > > Is this some "trick" that MSTest relies on?
>
> > > > > > > > > On Tue, Mar 24, 2009 at 12:26 PM, Jake <[email protected]>
> > > > wrote:
>
> > > > > > > > > > It's in Files in the RhinoMock Group (look on the
> > right-side)
>
> > > > > > > > > > On Mar 24, 3:25 pm, Chris Martin <[email protected]>
> > wrote:
> > > > > > > > > > > Where is that zip file?
>
> > > > > > > > > > > On Tue, Mar 24, 2009 at 12:23 PM, Jake <
> > [email protected]>
> > > > > > wrote:
>
> > > > > > > > > > > > I uploaded ProtectedMethodCase.zip into files (It works
> > in
> > > > > > > > > > > > VisualStudio 2008 with Microsoft Test Framework). I
> > > > implemented
> > > > > > the
> > > > > > > > > > > > test using the PartialMock as was suggested earlier.
>
> > > > > > > > > > > > On Mar 24, 3:20 pm, Tim Barcz <[email protected]>
> > wrote:
> > > > > > > > > > > > > It wasn't AAA, I was trying to clarify aruond
> > PartialMock
> > > > but
> > > > > > I
> > > > > > > > think
> > > > > > > > > > I
> > > > > > > > > > > > only
> > > > > > > > > > > > > muddied the waters....
>
> > > > > > > > > > > > > On Tue, Mar 24, 2009 at 2:19 PM, Tuna Toksoz <
> > > > > > [email protected]>
> > > > > > > > > > wrote:
> > > > > > > > > > > > > > I didin't realize it was AAA syntax, because I saw
> > > > > > ReplayAll,
> > > > > > > > and
> > > > > > > > > > > > though
> > > > > > > > > > > > > > the record/replay mode.
>
> > > > > > > > > > > > > > Tuna Toksöz
> > > > > > > > > > > > > > Eternal sunshine of the open source mind.
>
> > > > > > > > > > > > > >http://devlicio.us/blogs/tuna_toksoz
> > > > > > > > > > > > > >http://tunatoksoz.com
> > > > > > > > > > > > > >http://twitter.com/tehlike
>
> > > > > > > > > > > > > > On Tue, Mar 24, 2009 at 9:08 PM, Chris Martin <
> > > > > > > > > > [email protected]
> > > > > > > > > > > > >wrote:
>
> > > > > > > > > > > > > >> This won't even compile.
> > > > > > > > > > > > > >> Your methods are protected.
>
> > > > > > > > > > > > > >> Change them to public and this following passes.
>
> > > > > > > > > > > > > >> ---
> > > > > > > > > > > > > >> public class ClassA
> > > > > > > > > > > > > >> {
> > > > > > > > > > > > > >> public bool Success;
>
> > > > > > > > > > > > > >> public virtual bool FileExists(string
> > > > filename)
> > > > > > > > > > > > > >> {
> > > > > > > > > > > > > >> return File.Exists(filename);
> > > > > > > > > > > > > >> }
>
> > > > > > > > > > > > > >> public virtual void Process(string
> > filename)
> > > > > > > > > > > > > >> {
> > > > > > > > > > > > > >> Success = FileExists(filename);
> > > > > > > > > > > > > >> }
> > > > > > > > > > > > > >> }
>
> > > > > > > > > > > > > >> [TestFixture]
> > > > > > > > > > > > > >> public class Tests
> > > > > > > > > > > > > >> {
> > > > > > > > > > > > > >> [Test]
> > > > > > > > > > > > > >> public void process_test()
> > > > > > > > > > > > > >> {
> > > > > > > > > > > > > >> var mocks = new MockRepository();
>
> > > > > > > > > > > > > >> var a = mocks.PartialMock<ClassA>();
>
> > Expect.Call(a.FileExists(null)).IgnoreArguments().Return(true);
> > > > > > > > > > > > > >> mocks.ReplayAll();
>
> > > > > > > > > > > > > >> a.Process("test.test");
>
> > > > > > > > > > > > > >> Assert.AreEqual(a.Success, true);
> > > > > > > > > > > > > >> }
> > > > > > > > > > > > > >> }
>
> > > > > > > > > > > > > >> On Tue, Mar 24, 2009 at 10:50 AM, Jake <
> > > > [email protected]
>
> > > > > > > > wrote:
>
> > > > > > > > > > > > > >>> Sorry, for this mistake, but method is virtual!
> > > > > > > > > > > > > >>> Here is more info:
>
> > > > > > > > > > > > > >>> public class classA: baseClass
> > > > > > > > > > > > > >>> {
>
> > > > > > > > > > > > > >>> public bool Success;
>
> > > > > > > > > > > > > >>> protected virtual bool FileExists(string
> > fileName)
> > > > > > > > > > > > > >>> {
> > > > > > > > > > > > > >>> return File.Exists(fileName);
> > > > > > > > > > > > > >>> }
>
> > > > > > > > > > > > > >>> protected virtual void Process(string fileName)
> > > > > > > > > > > > > >>> {
> > > > > > > > > > > > > >>> //Do something
> > > > > > > > > > > > > >>> if(FileExists(fileName))
> > > > > > > > > > > > > >>> {
> > > > > > > > > > > > > >>> Success = true;
> > > > > > > > > > > > > >>> }
> > > > > > > > > > > > > >>> else
> > > > > > > > > > > > > >>> {
> > > > > > > > > > > > > >>> Success = false;
> > > > > > > > > > > > > >>> }
> > > > > > > > > > > > > >>> }
> > > > > > > > > > > > > >>> }
>
> > > > > > > > > > > > > >>> Unit test:
>
> > > > > > > > > > > > > >>> [TestMethod]
> > > > > > > > > > > > > >>> public void ProcessTest()
> > > > > > > > > > > > > >>> {
> > > > > > > > > > > > > >>> var a = _mocks.StrickMock<baseClass>();
>
> > > > Expect.Call(a.FileExists(null)).IgnoreArguments().Return(true);
> > > > > > > > > > > > > >>> _mocks.ReplayAll();
> > > > > > > > > > > > > >>> a.Process("test.test"); //File doesn't exists
> > > > > > > > > > > > > >>> Assert.AreEqual(true, a.Success); //Fails here
> > > > > > > > > > > > > >>> }
>
> > > > > > > > > > > > > >>> On Mar 24, 1:39 pm, Alex McMahon <
> > > > [email protected]>
> > > > > > > > wrote:
> > > > > > > > > > > > > >>> > Method2 must be a virtual method for RhinoMocks
> > to
> > > > be
> > > > > > able
> > > > > > > > to
> > > > > > > > > > mock
> > > > > > > > > > > > the
> > > > > > > > > > > > > >>> > method with a PartialMock,
>
> > > > > > > > > > > > > >>> > On Tue, Mar 24, 2009 at 5:21 PM, Jake <
> > > > > > [email protected]>
> > > > > > > > > > wrote:
>
> > > > > > > > > > > > > >>> > > Ayende,
> > > > > > > > > > > > > >>> > > I am trying to use Rhino Mocks in following
> > > > scenario:
>
> > > > > > > > > > > > > >>> > > void classA.Method1()
> > > > > > > > > > > > > >>> > > {
> > > > > > > > > > > > > >>> > > //some stuff
> > > > > > > > > > > > > >>> > > if(classA.Method2(var))
> > > > > > > > > > > > > >>> > > {
> > > > > > > > > > > > > >>> > > //Do something
> > > > > > > > > > > > > >>> > > }
> > > > > > > > > > > > > >>> > > else
> > > > > > > > > > > > > >>> > > {
> > > > > > > > > > > > > >>> > > //Do something else
> > > > > > > > > > > > > >>> > > }
> > > > > > > > > > > > > >>> > > //some other stuff
> > > > > > > > > > > > > >>> > > }
>
> > > > > > > > > > > > > >>> > > Now, I created partial mock for the class and
> > set
> > > > > > > > expectation
> > > > > > > > > > for
> > > > > > > > > > > > > >>> > > Method2 like so:
>
> > > > Expect.Call(classAMock.Method2(null)).IgnoreArguments().Return(true);
>
> > > > > > > > > > > > > >>> > > Real implementation of the method returns
> > false.
>
> > > > > > > > > > > > > >>> > > When I execute the test it goes through the
> > path
> > > > when
> > > > > > > > Method2
> > > > > > > > > > > > returns
> > > > > > > > > > > > > >>> > > false.
>
> > > > > > > > > > > > > >>> > > How can that be?
>
> > > > > > > > > > > > > >> --
>
> > > > > > > > > > > > > >> Charles Kettering - "My interest is in the future
> > > > because
> > > > > > I
> > > > > > > > am
> > > > > > > > > > going
> > > > > > > > > > > > to
> > > > > > > > > > > > > >> spend the rest of my life there."
>
> ...
>
> read more »
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---