views:

44

answers:

2

Hello. I'm diving into ASP.NET MVC 2 and I'm walking through a tutorial and I'm getting an error related to a template method in my unit tests. The erroneous code is...

var displayedProducts = (IList<Product>)result.ViewData.Model;
displayedProducts.Count.ShouldEqual(2);

and the method definition for ShouldEqual is...

public static void ShouldEqual<T>(this T actualValue, T expectedValue)
{
   Assert.AreEqual(expectedValue, actualValue);
}

and the error is...

'int' does not contain a definition for 'ShouldEqual' and no extension method 'ShouldEqual' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?)

but because I'm so new to all this, I don't see what I missing.

  1. Does anyone see the problem?
  2. Can someone explain to me how ShouldEqual is a member function of Count

Thanks so much for your help! If more code is needed, please let me know.

+1  A: 

ShouldEqual is compiler trick only. It's a stand alone function (static class (CLASSNAME) and method). The compiler replaces the call with...

CLASSNAME.ShouldEqual<int>(displayedProducts.Count, 2);

See here for more information : http://msdn.microsoft.com/en-us/library/bb383977.aspx

Preet Sangha
+1  A: 

I recognize this... this is from the Steve Sanderson book?

I can reproduce this error if I comment out the using statement for the namespace where ShouldEqual is declared.

In the file where you have

var displayedProducts = (IList<Product>)result.ViewData.Model;
displayedProducts.Count.ShouldEqual(2);

can you check and make sure you have a using statement for the namespace that ShouldEqual is in?

adrift
nice catch! that's exactly the book it's from. anyhow, thanks for the post, and I noticed I don't have a "using" statement for the namespace that includes "ShouldEqual" because both blocks of code exist in the same namespace (SportsStore.UnitTests), but they exist in two different files. I'm not sure how to handle this. Your thoughts? Thanks again!
BeachRunnerJoe
Off the top of my head, I don't recall how the projects are structured. Is `ShouldEqual` in a different project than the other 2 lines of code? If it is, try adding a reference from that project to the project where `ShouldEqual` is defined.
adrift
Hmm, that's strange. I just found where you are in the book, and they are in the same project, should be in the same namespace... I don't see any differences in your code and the code in the book. Can you download the code from the apress site (see 'Book Resources' in the bottom left of this page: http://www.apress.com/book/downloadfile/4609) and try building it? It builds fine for me, so maybe looking at that code and your project will help you figure out what is different.
adrift
thanks, adrift, yeah i'll try that. i really appreciate your effort with this!
BeachRunnerJoe
You're welcome and I hope the code from the site helps you out. Good luck!
adrift