How to use DelayedConstraint

611 views
Skip to first unread message

Juan Manuel Truppia

unread,
Feb 15, 2012, 7:23:32 AM2/15/12
to nunit-...@googlegroups.com
It didn't seem clear in the docs to me, we don't have an example

Suppose I have this

Thread.Sleep(65*1000);
Assert.That(() => foo(),Throws.Nothing);

Is this the same as

Assert.That(() => foo(),Throws.Nothing.After(65 * 1000))

???

Cheers

Charlie Poole

unread,
Feb 15, 2012, 1:18:13 PM2/15/12
to nunit-...@googlegroups.com
Hi Juan,

On Wed, Feb 15, 2012 at 4:23 AM, Juan Manuel Truppia
<jmtr...@gmail.com> wrote:
> It didn't seem clear in the docs to me, we don't have an example
>
> Suppose I have this
>
> Thread.Sleep(65*1000);
> Assert.That(() => foo(),Throws.Nothing);

This means
1. Sleep for 65 seconds
2. call foo()
3. Assert that it didn't throw

> Is this the same as
>
> Assert.That(() => foo(),Throws.Nothing.After(65 * 1000))

This means
1. Wait 65 seconds
2. Call the lambda, which calls foo()
3. Assert that it didn't throw.


So they are essentially the same.

Note that you can also use After with a second argument, the polling
interval, in which case the full 65 second wait may not be needed.
In that case, the method must be one that can be called repeatedly
without causing a problem.

Your use with Throws.Nothing doesn't seem to make a lot of sense,
however. The normal sequence for which we want to use this
constraint is:

1. Take an action
2. Wait a period of time
3. Observe the result

For example,
worker.Start();
Assert.That( worker.State, Is.EqualTo(WorkerState.Idle).After(30000);

Charlie

> ???
>
> Cheers
>
> --
> You received this message because you are subscribed to the Google Groups
> "NUnit-Discuss" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/nunit-discuss/-/_l06k5ggf54J.
> 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.

Juan Manuel Truppia

unread,
Mar 12, 2012, 8:26:36 AM3/12/12
to nunit-...@googlegroups.com
Charlie, I didn't get the same results with this.
1- With the sleep, I first wait, and then do the action
2- With the After constraint, I first do the action, and then sleep (waiting to check if an exception is raised)

I required behavior number 1, so I had to go with the Sleep. Does NUnit provide some way to implement behavior 1?


On Wednesday, February 15, 2012 3:18:13 PM UTC-3, charlie wrote:
Hi Juan,

On Wed, Feb 15, 2012 at 4:23 AM, Juan Manuel Truppia

Charlie Poole

unread,
Mar 12, 2012, 12:19:56 PM3/12/12
to nunit-...@googlegroups.com
Hi Juan,

On Mon, Mar 12, 2012 at 5:26 AM, Juan Manuel Truppia <jmtr...@gmail.com> wrote:
Charlie, I didn't get the same results with this.
1- With the sleep, I first wait, and then do the action
2- With the After constraint, I first do the action, and then sleep (waiting to check if an exception is raised)

Well, yes. That's what my note said.
 
I required behavior number 1, so I had to go with the Sleep. Does NUnit provide some way to implement behavior 1?

Behavior #1 is available through a Sleep, so NUnit didn't have to provide it. Behavior #2 is not available without writing
more lines of code, so NUnit encapsulates it.

Charlie
 
To view this discussion on the web visit https://groups.google.com/d/msg/nunit-discuss/-/zZkYUN272mIJ.

To post to this group, send email to nunit-...@googlegroups.com.
To unsubscribe from this group, send email to nunit-discus...@googlegroups.com.

jmtr...@gmail.com

unread,
Mar 12, 2012, 12:26:42 PM3/12/12
to nunit-...@googlegroups.com
Sorry, I had understood that both behaviors were the same. Thanks Charlie
From: Charlie Poole <nuni...@gmail.com>
Date: Mon, 12 Mar 2012 09:19:56 -0700
Subject: Re: [nunit-discuss] How to use DelayedConstraint
Reply all
Reply to author
Forward
0 new messages