How do you access public and protected variables in the same test?

T

Tom P.

I am starting to get into the Unit Testing in VS2008 and I have a
question: How do I access a public field and a private field in the
same object? I have seen the _Accessor and I can use it to execute a
protected method but I can't Assert the test passes because the field
I should check is public and not included in the _Accessor object.

So what do I do here? How do I create an object that allows me to
access the public AND protected members?

Thanks in advance,
Tom P.
 
M

Mr. Arnold

Tom P. said:
I am starting to get into the Unit Testing in VS2008 and I have a
question: How do I access a public field and a private field in the
same object? I have seen the _Accessor and I can use it to execute a
protected method but I can't Assert the test passes because the field
I should check is public and not included in the _Accessor object.

So what do I do here? How do I create an object that allows me to
access the public AND protected members?

Protected variables, are variables that are visible only to the class to
which they belong, and any subclasses.

You create a Public function, a class, which can manipulate the protected
variable in a controlled way.

The Assert can be used against a function.

__________ Information from ESET NOD32 Antivirus, version of virus signature
database 4540 (20091025) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com




__________ Information from ESET NOD32 Antivirus, version of virus signature database 4540 (20091025) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
T

Tom P.

Protected variables, are variables that are visible only to the class to
which they belong, and any subclasses.

You create a Public function, a class,  which can manipulate the protected
variable in a controlled way.

The Assert can be used against a function.

__________ Information from ESET NOD32 Antivirus, version of virus signature
database 4540 (20091025) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4540 (20091025) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

Then what are the _Accessor objects for? They expose all the methods
and variables that are internal (protected or private) or friend. I
have access to the private and protected fields, but the _Accessor
object doesn't expose the public variables. So, I can access either
internal fields or public fields, but not both in the same object.

I appreciate how to access an internal method but I'm looking for how
to TEST an internal method. If I work around testing how will I count
the testing as valid?

So, still I'm looking for a way to access the public fields when using
a protected accessor in a [TestClass].

Tom P.
 
M

Mr. Arnold

Protected variables, are variables that are visible only to the class to
which they belong, and any subclasses.

You create a Public function, a class, which can manipulate the protected
variable in a controlled way.

The Assert can be used against a function.

Then what are the _Accessor objects for? They expose all the methods
and variables that are internal (protected or private) or friend. I
have access to the private and protected fields, but the _Accessor
object doesn't expose the public variables. So, I can access either
internal fields or public fields, but not both in the same object.

I appreciate how to access an internal method but I'm looking for how
to TEST an internal method. If I work around testing how will I count
the testing as valid?

So, still I'm looking for a way to access the public fields when using
a protected accessor in a [TestClass].


If you want to test object behavior, then you must mock the object with a
proper unit testing tool.

http://en.wikipedia.org/wiki/Mock_object
http://www.ayende.com/projects/rhino-mocks.aspx


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4541 (20091025) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
T

Tom P.

If you want to test object behavior, then you must mock the object with a
proper unit testing tool.

http://en.wikipedia.org/wiki/Mock_objecthttp://www.ayende.com/projects/rhino-mocks.aspx

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4541 (20091025) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

Yes, I know. That is exactly what I am asking.

How do I properly mock an object in VS2008 so that I have access to
both the public and internal fields and methods.

Have you ever used Visual Studio 2008? Have you ever used the testing
tools that come in it? Those are the tools I am trying to use to solve
this problem. If you have any experience with this in VS2008 I'd
appreciate some help.

Tom P.
 
M

Mr. Arnold

Tom P. said:
How do I properly mock an object in VS2008 so that I have access to
both the public and internal fields and methods.

http://www.ayende.com/projects/rhino-mocks.aspx
http://www.codeproject.com/KB/dotnet/Rhino_Mocks.aspx
http://www.google.com/search?hl=en&source=hp&q=rhino+mock+tutorial&aq=0&oq=rhino+mock+&aqi=g10


Have you ever used Visual Studio 2008? Have you ever used the testing
tools that come in it? Those are the tools I am trying to use to solve
this problem. If you have any experience with this in VS2008 I'd
appreciate some help.

Yes, I develop solutions using VS 2008. I do unit testing with MBUnit (part
of .NET), Rhino Mocks (free BSD), Gallio (free BSD) and Resharper (not
free).

Except for Resharper, MBUnit is in the .Net Framework, and you can download
the (free BSD) tools and install them as plug-ins to VS 2008. Resharper you
can download that is also a VS 2008 plug-in and use the full trial version
just to see it in action, which I purchased for my personal usage

Mocking tools are 3rd party solutions that I know about and Rhino Mocks is
not the only one.

You have the links above, and you can find articles using Bing or Google on
how to do things with the tools, as you're going to have to learn that way,
if you can't attend a class on it.




__________ Information from ESET NOD32 Antivirus, version of virus signature database 4541 (20091025) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
T

Tom P.

Yes, I develop solutions using VS 2008. I do unit testing with MBUnit (part
of .NET),  Rhino Mocks (free BSD), Gallio (free BSD) and Resharper (not
free).

Except for Resharper, MBUnit is in the .Net Framework,  and you can download
the (free BSD) tools and install them as plug-ins to VS 2008. Resharper you
can download that  is also a VS 2008 plug-in and use the full trial version
just to see it in action, which I purchased for my personal usage

Mocking tools are 3rd party solutions that I know about and Rhino Mocks is
not the only one.

You have the links above,  and you can find articles using Bing or Google on
how to do things with the tools, as you're going to have to learn that way,
if you can't attend a class on it.

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4541 (20091025) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

OK, thanks. Since I'm not using those tools I think I'll move on. I'm
using the Unit Testing classes built into Visual Studio and don't yet
see the argument for changing that.

Since this has been pushed to the second page, and the question has
been obfuscated quite a bit by the detour, I'll repost to get a little
more attention for my original question.

Thanks for the efforts.

Tom P.
 
T

Tom P.

Tom P. wrote:

<snipped>




The test harness that VS provides cannot mock objects or properties of
objects and is where you are stuck at, and you cannot complete the tests
nor can you automate the tests either, which is the point of unit
testing -- automated testing.




You're not coming past it, as you cannot mock objects without a 3rd
party tool, and you cannot address protected properties of an object,
unless you do object and property mocking for expected results.

You need to take a class on the subject, like I spent 8 hours in a
company paid class to get the basics of unit testing and the tools
required to do unit testing.

You can lead the horse to the water, but one cannot make the horse
drink. The horse has to drink for itself.

OK, let me put it this way - you're not helping.

I've spent two years as a test team lead and have written tests using
nUnit, WinRunner and other platforms and I do understand what I am
asking. Please appreciate that there are different ways to test and I
am proceeding down a road you have obviously discounted. Please
understand that, your wonderful 8 hours aside, there are different
ways to approach testing and I am not using the one you are talking
about.

Thank you for your efforts.

Tom P.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top