开发者

Why is my C# template method complaining about accepting a specific type?

开发者 https://www.devze.com 2023-01-17 23:37 出处:网络
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...

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?)开发者_如何学C

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.


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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消