Using Assert.throws() in vb.net

2,291 views
Skip to first unread message

authchir

unread,
Jun 29, 2010, 10:41:25 PM6/29/10
to NUnit-Discuss
Hi group,
I have been searching on the web for about two hours and a half and
can't find what I am looking for. We are using vb.net in my
programming class and I want to use Nunit to test the project we are
working on. The problem is that I am not able to use the
Assert.throws() methode. For example :

Public Class Custumer

private _id as integer

Property id() as integer
Get
Return _id
End Get

Set(ByVal value as integer)
If value > 10 then
throw New Exception("Max value is 10")
Else
_id = value
End if
End Set

End Property

public sub takeAGuess(ByVal nbr as integer)
If nbr = 5 then
throw New Exception("Bad luck")
Else
End sub

End Class

How would I try each mehtodes (the property and the sub) with
Assert.throws ?
I've found some examples on the net, but they were all in C#.

Authchir

authchir

unread,
Jul 9, 2010, 8:19:35 PM7/9/10
to NUnit-Discuss
So far, I have able to make an assert like this:
Assert.Throws(Of Exception)(new TestDelegate(AddressOf
subWithNoParameters))

How can we test function that takes parameters ?

Authchir

Charlie Poole

unread,
Jul 9, 2010, 8:39:53 PM7/9/10
to nunit-...@googlegroups.com
I can imagine two ways..

Create an anonymous delegate that calls the method you want

Create a lambda that does the same

It would be great if some VB experts chimed in here with the syntax!

Charlie

> --
> You received this message because you are subscribed to the Google Groups "NUnit-Discuss" group.
> To post to this group, send email to nunit-...@googlegroups.com.
> To unsubscribe from this group, send email to nunit-discus...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nunit-discuss?hl=en.
>
>

Yann Duran

unread,
Jul 9, 2010, 10:14:49 PM7/9/10
to nunit-...@googlegroups.com
Hi Authchir,

I've only used Assert.Throws with *functions* that have parameters. If you
can make your sub a function, this may help:

Assert.Throws(Of Exception)(Function()
FunctionWithParameters(parameterValue))

I've only used subs *without* parameters:

Assert.Throws(GetType(Exception), AddressOf SubWithNoParameters)

But if you're using ,NET 4, this should also work:

Assert.Throws(Of Exception)(Sub() SubWithParameters(parameterValue))

Hope that helps even a little bit,

Yann

--------------------------------------------------
From: "authchir" <martin.d...@gmail.com>
Sent: Saturday, July 10, 2010 10:19 AM
To: "NUnit-Discuss" <nunit-...@googlegroups.com>
Subject: [nunit-discuss] Re: Using Assert.throws() in vb.net

authchir

unread,
Jul 10, 2010, 9:52:48 AM7/10/10
to NUnit-Discuss
Great, it works !!!
It is true that I usually expect a return when I pass arguments, so
function are more appropriated.

The last question is how to test that the set property of my class
attribute throw an exception?
The following syntax doesn't seam to work :
Assert.Throws(Of Exception)(Function() custumer.id = 100)

Authchir

Yann Duran

unread,
Jul 11, 2010, 9:45:44 PM7/11/10
to nunit-...@googlegroups.com
Hi Authchir,

The only way I can think of to do this would be to create a private function
in your test code that sets the property. Normally I wouldn't test
getters/setters, but I'm guessing you have some custom code in it.

'in your test method
Assert.Throws(Of Exception)(Function() SetCustomerID(100))

'somewhere in the test fixture
private function SetCustomerID(value as integer) as boolean
custumer.id = value
end function

But what I'd suggest you do is move the verification code out of the setter,
so it becomes testable & call it from the setter instead of having the code
in the setter itself.

Yann

--------------------------------------------------
From: "authchir" <martin.d...@gmail.com>
Sent: Saturday, July 10, 2010 11:52 PM


To: "NUnit-Discuss" <nunit-...@googlegroups.com>
Subject: [nunit-discuss] Re: Using Assert.throws() in vb.net

> Great, it works !!!

Reply all
Reply to author
Forward
0 new messages