Assert.Equals(DataTable, MethodCall());

1,051 views
Skip to first unread message

Axonn Echysttas

unread,
Feb 2, 2009, 9:27:04 AM2/2/09
to NUnit-Discuss
Hello everybody! My first message here. I'm a C# programmer who just
began taking seriously nUnit and TDD a few weeks ago.

I am wondering how to do an Assert in nUnit when I got a method which
returns a DataTable (or DataSet). There are C# classes which are
usually employed when working with databases.

My TDD application also uses a MySQL database. I will mock-up the
results returned by MySQL by returning a DataTable built by myself.
But how to Assert? Do I need to build a custom Matcher for nUnit or
does it do it by itself?

Thank you in advance!

Brad Stiles

unread,
Feb 2, 2009, 1:07:22 PM2/2/09
to nunit-...@googlegroups.com
> I am wondering how to do an Assert in nUnit when I got a method which
> returns a DataTable (or DataSet). There are C# classes which are
> usually employed when working with databases.

I'll repeat my question. ;) What is it that you want to assert? Are
you trying to make sure that the data set that comes out of your
method looks like what you want it to? If so, the simplest method
might simply be to assert each attribute that you want to check, one
at a time, and then write code that satisfies each assert, one at a
time.

For instance, if the result you expect is a dataset with three tables,
"TableOne", "TableTwo" and "TableThree", then I might start with that,
e.g. Assert.That(result.Tables.Count, Is.EqualTo(3)), and then move on
to the next attribute, e.g. that the Tables collection contains
"TableOne", and so on, until all is as expected.

/bs

Axonn Echysttas

unread,
Feb 2, 2009, 1:46:25 PM2/2/09
to NUnit-Discuss
Hey, thanks for answering. As I said...

To tell you the truth, I didn't yet write the code or the tests, but I
know answers are hard to find so I asked before I reached the actual
coding. When I reach the necessity to Assert.Equals(dataTable,
MethodThatReturnsDataTable()); I want to also have the answer about
how to do it. Anyway, apparently nobody wants to tell me the
secret ::- D. I also searched before asking on Google and found this

http://vanryswyckjan.blogspot.com/2007/04/nunit-24-constraints.html

Now maybe this is an indication nUnit can't do that kind of test
without helping it a little bit. I thought that maybe somebody here
knows about a newer nUnit version that can.

Axonn.

Brad Stiles

unread,
Feb 2, 2009, 2:04:23 PM2/2/09
to nunit-...@googlegroups.com
> When I reach the necessity to Assert.Equals(dataTable, MethodThatReturnsDataTable());

That's part of my point. You've decided that you need both a
DataTable *and* an Equals assertion for DataTables before you get to
the point where you actually use or need either one. Considerably
before that point, in fact. That does not fit in very well with what
I now understand of TDD.

I'll freely admit to the same mistake when I started out. I *knew* my
system was going to need certain things, so I built them, using TDD,
and thought I was doing well. Until the time came when I found out
that what I'd built had little or nor relation to what I ended up
actually needing elsewhere later on, and I had to go back and change
it. Scrapped it and started over, I did, since that turned out to be
the easiest thing to do.

In addition, "Equals" in this case has a multiple potential meanings,
e.g. object reference A equals object reference B (which would be
quite useless for your purposes, or what I can see of them) or all
properties of A equal all properties of B, which is what I suspect you
are trying to get at.

Yes, it would be nice to put together one single DataTable equals
assertion, but I've seen no compelling evidence to this point that you
actually need such a thing, especially not at this early point. There
are a number of tiny little asserts you could do that would eventually
add up to the assertion that one data table is equal to another,
without having to come up with that exact thing right up front.

In My Never Even Close To Humble Opinion, starting small is the best
way to start with TDD, and this is definitely not small.

/bs

Charlie Poole

unread,
Feb 2, 2009, 2:04:21 PM2/2/09
to nunit-...@googlegroups.com
Hi Axonn,

Can you tell us what you want to assert about the return
value? If it's about certain contents of the DT, then
standard calls will work fine, but give us a few
examples and we'll try to help you.

Charlie

Axonn Echysttas

unread,
Feb 2, 2009, 3:41:45 PM2/2/09
to NUnit-Discuss
Brad: while I know you're totally right, I still stubornly want to
know if nUnit can do that assert! ::- D. But please, don't take this
the wrong way! I thank you very much for what you said and I learned
my lesson and I will NOT TRY to do that UNLESS a succession of ever-
more-complex tests lead me there! But now I still want to know if
nUnit can do it ::- ).

Charlie & Brad: when I say I want to assert that two DataTables are
equal, I mean EVERY SINGLE VALUE TO BE EXACLTY THE SAME. The expected
DT should have the same columns, rows and values as the DT returned.
And I don't know if that works with Assert. That was the question,
yes.

Charlie Poole

unread,
Feb 2, 2009, 3:43:54 PM2/2/09
to nunit-...@googlegroups.com
Hi Axxon,

I'll just repeat my answers from the other list...

> Hey, thanks for answering. As I said...
>
> To tell you the truth, I didn't yet write the code or the
> tests, but I know answers are hard to find so I asked before
> I reached the actual coding. When I reach the necessity to
> Assert.Equals(dataTable, MethodThatReturnsDataTable()); I
> want to also have the answer about how to do it.

OK, that's a step toward what folks have been asking for:
what do you want to do? You are saying: to assert equality between two data
tables! NUnit will let you write that statement, but theresult depends on
how a DataTable defines equality. A quick look at the .NET docs tells me
that Equals is inerited from Object, which gives you reference equality.
I'm pretty sure that's not what you want.

So, the next step is "What do you want equality to mean?"
This could have several possible answers, which is probably why the
developers of .NET didn't override object equality.

> Anyway,
> apparently nobody wants to tell me the secret ::- D. I also
> searched before asking on Google and found this
>
> http://vanryswyckjan.blogspot.com/2007/04/nunit-24-constraints.html
>
> Now maybe this is an indication nUnit can't do that kind of
> test without helping it a little bit. I thought that maybe
> somebody here knows about a newer nUnit version that can.

That's a somewhat out of date (pre-2.4) description of how to extend NUnit.
I'm fairly certain that you could extend NUnit to do what you want, if you
can define what that is. But the first step should be to see if it already
does what you want.

Axonn Echysttas

unread,
Feb 2, 2009, 3:46:17 PM2/2/09
to NUnit-Discuss
In case this helps... this is A THEORETICAL EXAMPLE OF THE TEST.

I am curious only if this is possible in nUnit.

Database = new mockup();
Presenter = new mockup();
MyDataTable = new DataTable();
.............
code where I write some hardcoded data in MyDataTable.
.............
Expect.Call(Database.Method()).Returns(MyDataTable);
Assert.Equals(MyDataTable, Presenter.DoSomethingToTheDataTable
(MyDataTable));

This is the general idea. To check if the presenter DOES something to
that DataTable. I got a method that does something to it.



On Feb 2, 9:04 pm, "Charlie Poole" <char...@nunit.com> wrote:

Axonn Echysttas

unread,
Feb 2, 2009, 3:53:16 PM2/2/09
to NUnit-Discuss
And no, reference equality is definitely not what I need since it
won't say if the values are the same in the two datasets.

I know how equality is defined in .Net but I thought that maybe nUnit
already knows to "hand parse" a DataSet. If not, I can do it, of
course. My link contained some sort of method to do that.

Charlie Poole

unread,
Feb 2, 2009, 3:56:30 PM2/2/09
to nunit-...@googlegroups.com
Hi Axxon,

> Brad: while I know you're totally right, I still stubornly
> want to know if nUnit can do that assert! ::- D. But please,
> don't take this the wrong way! I thank you very much for what
> you said and I learned my lesson and I will NOT TRY to do
> that UNLESS a succession of ever- more-complex tests lead me
> there! But now I still want to know if nUnit can do it ::- ).

As I said in another post, yes we can! But I can't imagine
that it will be very useful to you.

> Charlie & Brad: when I say I want to assert that two
> DataTables are equal, I mean EVERY SINGLE VALUE TO BE EXACLTY
> THE SAME. The expected DT should have the same columns, rows
> and values as the DT returned.
> And I don't know if that works with Assert. That was the
> question, yes.

With a few exceptions (and this is not one) NUnit uses the same
meaning for Equality as the type you are dealing with. Since
DataTable does not override equality to mean what you want,
but inherits Equals from System.Object, EqualTo(DataTable)
will not do what you want.

What's more, this will probably never be implemented as one
of NUnit's "special handling" rules for certain types. That's
because there is no general approach that would satisfy
everyone - which is probably why .NET doesn't do it either.

That said, you can write asserts for /each/ of the things
you want to check. And since some of them relate to enumerables,
you may be able to find expressive ways to do it using
NUnit's syntax for asserting over enumerables. A good way
to start is to write the "brute force" code that asserts
on each property, one at a time. Then ask us if some of it
can be improved.

I want to add one thing, however... You stated originally
that you were new to using NUnit and TDD - or at least had
not done it very seriously before. But your first questions
were about Gui, Database and NUnit extensions. If I were
your coach, I would be pressing you to write some simple
tests of POCOs for a few weeks before moving on to these
things that most people consider advanced topics.

Charlie
Charlie

Axonn Echysttas

unread,
Feb 2, 2009, 3:59:10 PM2/2/09
to NUnit-Discuss
I'll get back to the drawing board and come with some real code! ::- )

Charlie Poole

unread,
Feb 2, 2009, 4:02:59 PM2/2/09
to nunit-...@googlegroups.com
Hi Axxon,

> In case this helps... this is A THEORETICAL EXAMPLE OF THE TEST.

As a trainer, my experience is that people who give me theoretical
examples, rather than just writing tests, often don't learn TDD
very well. There are exceptions of course and I hope you will
be one of them.

> I am curious only if this is possible in nUnit.
>
> Database = new mockup();
> Presenter = new mockup();
> MyDataTable = new DataTable();
> .............
> code where I write some hardcoded data in MyDataTable.
> .............
> Expect.Call(Database.Method()).Returns(MyDataTable);
> Assert.Equals(MyDataTable, Presenter.DoSomethingToTheDataTable
> (MyDataTable));
> This is the general idea. To check if the presenter DOES
> something to that DataTable. I got a method that does something to it.

It's possible, but it only tests that the same datatable that was
given to the mock is returned by the datatable. You haven't
tested that the "something" was done correctly, so you would
have to add further asserts to look for modifications. But you
could do that without the equality assert on DT anyway.

Charlie
Reply all
Reply to author
Forward
0 new messages