I was able to get this to by changing the scope of some of the methods. I
don't get (or trust) ClassA_Accessor...if given the choice I would recommend
you use NUnit or MbUnit as your test framework and NOT MSTest (for reasons
such as what we're seeing here).
[TestMethod]
public void TestMethod1()
{
MockRepository _mocks = new MockRepository();
ClassA _classMock = _mocks.PartialMock<ClassA>();
Expect.Call(_classMock.FileExists(null)).IgnoreArguments().Return(true);
_mocks.ReplayAll();
_classMock.ProcessItem(1);
Assert.AreEqual(true, _classMock.Success);
}
public abstract class baseClass
{
public void Start()
{
for (int i = 0; i < 10; i++)
{
ProcessItem(i);
}
}
public abstract void ProcessItem(int i);
}
public class ClassA: baseClass
{
public bool Success;
public virtual bool FileExists(string fileName)
{
return File.Exists(fileName);
}
public override void ProcessItem(int i)
{
string fileName = "test " + i.ToString() + ".test";
Success = FileExists(fileName);
}
}
On Tue, Mar 24, 2009 at 2:33 PM, Tuna Toksoz <[email protected]> wrote:
> Same here, couldn't find the class file.
>
> 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: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."
>>> >
>>> > --
>>> >
>>> > Isaac Asimov - "I do not fear computers. I fear the lack of them."
>>>
>>>
>>
>>
>> --
>>
>> Charles Kettering - "My interest is in the future because I am going to
>> spend the rest of my life there."
>>
>>
>>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---