Following the sample ASP.NET MVC 3 Solution, SportsStore, in the APress Book, Pro ASP.NET MVC 3 Framework, Third Edition,
I am working on this solution using VS 2010. After creating Solution and Projects, adding References, and setting up DI container,
I was creating the Domain Model. The paragraph below from the book described how to make a Mock Repository. However, on my
NinjectControllerFactory.cs, there were 7 errors for invalid arguments, and 'Product' not found, etc.:
I
know we have a Product.cs model class under the Entities subfolder, so what wrong with the mock repository?
mock.Setup(m => m.Products).Returns(newList
<Product> { new
Product { Name = "Football",
Price = 25 }, new
Product { Name = "Surf
board", Price = 179 }, new
Product { Name = "Running
shoes", Price = 95 } }.AsQueryable());
Making a Mock Repository Now that we have defined an abstract interface, we could go ahead and implement the persistence mechanism and hook it up to a database.
We are going to do that later in this chapter. In order to be able to start writing other parts of the application, we are going to create a mock
implementation of the IProductRepository interface. We are going to do this in the
AddBindings
method of our
NinjectControllerFactory
class, as shown in Listing 7-5. Listing 7-5. Adding the Mock IProductRepository Implementation
private void AddBindings() {
// Mock implementation of the IProductRepository Interface
Mock<IProductRepository> mock = new Mock<IProductRepository>();
mock.Setup(m => m.Products).Returns(new List<Product> {
new Product { Name = "Football", Price = 25 },
new Product { Name = "Surf board", Price = 179 },
new Product { Name = "Running shoes", Price = 95 }
}.AsQueryable());
ninjectKernel.Bind<IProductRepository>().ToConstant(mock.Object);
}
Visual Studio will be able to resolve the namespaces of all of the new types in these statements, but you’ll need to add a
using
statement to import the
System.Linq
namespace in order to get access to the
AsQueryable
extension method.
Thanks.
As shown in my previous post, I did not miss the line. Anyway, running the project, displaying the
error message enclosed below. in addition to the 7 errors on the Visual Studio desktop. Also, I found
2 warnings, "Warning The 'packages' element is not declared", on the 2 packages.config files of the
SportsStore.WebUI and SportsStore.Domain projects.
Why? In my MVC project, I did not see the line 1 of the file global.asax, indicated in the Parser Error.
This line seems to be the Page line for the old ASP.NET aspx files.
Description:
An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: Could not load type 'SportsStore.WebUI.MvcApplication'.
Source Error:
Line 1: <%@ Application Codebehind="Global.asax.cs" Inherits="SportsStore.WebUI.MvcApplication" Language="C#" %>
BTW, on the website for this APress website,
http://www.apress.com/9781430234043, Errata page listed
the follwoing comment. I have the simialr question on "Adding References". It seemed to me something
is wrong with the project depedences. I used the "Package Manager Console" to add the 4 .packages
On page 5:
<div>In Chapter 7 section “Adding References” (page 5) you describe the commands that we must use if we want to add the required project dependencies, Ninject and Moq, using the Visual Studio Package Manager Console.
If you read the table 7-2, it seems that the info doesn’t match with the result of executing the previous commands.
Additionally, as a comment, the figure 7-15 (page 19 of the same Chapter) has a look & feel that doesn’t correspond with the rest of the images in the chapter.</div>
I was stuck on the this same issue, and found that if you right click on SportsStore.Domain than select Rebuild it correct these errors. There is a problem when you build the entier solution with all three project which brings the errors back! I still don't
know why this is happening.
I added a reference to Moq within SportsStore.WebUI, which corrected the error I was getting when trying to use the Mock<> method. According to the book SportsStores.UnitTests was the only project needing this reference.
I found this on Stack Overflow that was not mentions in the text. Simply added a reference to the SportsStore.Domain in your SportsStore.WebUI, and then add all of these using statements. None of this was part of the steps in the book.
A quick method to check if you're missing a using staement is to click on the piece of code causing the error and press "Ctrl + .' (That's Control and a period). Visual Studio will usually be able to find the using reference you're missing and add it it
to the file.
Thanks but I am aware of that Little trick and it is a nice feature. In my case the class methods were located in another project within the same solution. The designer does not give you the ability to CTL + . and find the correct reference it only ask if
you need to create a new class. By added this using statement SportsStore.Domain; I gained access to the classes and methods within that namespace I was missing. I could have added it to the web.config namespace tag as a global reference that probably
would have solved a lot of trouble. I'd never worked with multiple project in the same solution, and was not aware of that trick until later on.
Error 1 'System.Collections.Generic.List<SportsStore.Domain.Entities.Product>' does not contain a definition for 'AsQueryable' and no extension method 'AsQueryable' accepting a first argument of type 'System.Collections.Generic.List<SportsStore.Domain.Entities.Product>'
could be found (are you missing a using directive or an assembly reference?) c:\users\nick\documents\visual studio 2012\Projects\SportsStore\SportsStore.WebUI\Infrastructure\NinjectControllerFactory.cs 35 3 SportsStore.WebUI
wonjartran
Participant
907 Points
1214 Posts
Errors on making mock repository
Mar 16, 2012 04:14 PM|LINK
Following the sample ASP.NET MVC 3 Solution, SportsStore, in the APress Book, Pro ASP.NET MVC 3 Framework, Third Edition,
I am working on this solution using VS 2010. After creating Solution and Projects, adding References, and setting up DI container,
I was creating the Domain Model. The paragraph below from the book described how to make a Mock Repository. However, on my
NinjectControllerFactory.cs, there were 7 errors for invalid arguments, and 'Product' not found, etc.:
I know we have a Product.cs model class under the Entities subfolder, so what wrong with the mock repository?
mock.Setup(m => m.Products).Returns(new List <Product> {
new Product { Name = "Football", Price = 25 },
new Product { Name = "Surf board", Price = 179 },
new Product { Name = "Running shoes", Price = 95 }
}.AsQueryable());
Making a Mock Repository
Now that we have defined an abstract interface, we could go ahead and implement the persistence mechanism and hook it up to a database.
We are going to do that later in this chapter. In order to be able to start writing other parts of the application, we are going to create a mock
implementation of the IProductRepository interface. We are going to do this in the AddBindings method of our NinjectControllerFactory class, as shown in Listing 7-5.
Listing 7-5. Adding the Mock IProductRepository Implementation
private void AddBindings() {
// Mock implementation of the IProductRepository Interface
Mock<IProductRepository> mock = new Mock<IProductRepository>();
mock.Setup(m => m.Products).Returns(new List<Product> {
new Product { Name = "Football", Price = 25 },
new Product { Name = "Surf board", Price = 179 },
new Product { Name = "Running shoes", Price = 95 }
}.AsQueryable());
ninjectKernel.Bind<IProductRepository>().ToConstant(mock.Object);
}
Visual Studio will be able to resolve the namespaces of all of the new types in these statements, but you’ll need to add a using statement to import the System.Linq namespace in order to get access to the AsQueryable extension method.
imauld
Member
145 Points
85 Posts
Re: Errors on making mock repository
Mar 16, 2012 04:38 PM|LINK
I have the same book and if that's all you ahve for the mock repository you're missing a line of code:
wonjartran
Participant
907 Points
1214 Posts
Re: Errors on making mock repository
Mar 16, 2012 07:46 PM|LINK
Thanks.
As shown in my previous post, I did not miss the line. Anyway, running the project, displaying the
error message enclosed below. in addition to the 7 errors on the Visual Studio desktop. Also, I found
2 warnings, "Warning The 'packages' element is not declared", on the 2 packages.config files of the
SportsStore.WebUI and SportsStore.Domain projects.
Why? In my MVC project, I did not see the line 1 of the file global.asax, indicated in the Parser Error.
This line seems to be the Page line for the old ASP.NET aspx files.
packages.config
<?xml version="1.0" encoding="utf-8" ?>
<packages>
<package id="jQuery" version="1.5.1"/>
<package id="jQuery.vsdoc" version="1.5.1"/>
<package id="jQuery.Validation" version="1.8.0"/>
<package id="jQuery.UI.Combined" version="1.8.11"/>
<package id="EntityFramework" version="4.1.10331.0"/>
<package id="Modernizr" version="1.7"/>
<package id="Ninject" version="2.2.1.4"/>
<package id="Moq" version="4.0.10827"/>
</packages>
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: Could not load type 'SportsStore.WebUI.MvcApplication'.
Source Error:
Line 1: <%@ Application Codebehind="Global.asax.cs" Inherits="SportsStore.WebUI.MvcApplication" Language="C#" %>Source File: /global.asax Line: 1
wonjartran
Participant
907 Points
1214 Posts
Re: Errors on making mock repository
Mar 16, 2012 08:05 PM|LINK
BTW, on the website for this APress website, http://www.apress.com/9781430234043, Errata page listed
the follwoing comment. I have the simialr question on "Adding References". It seemed to me something
is wrong with the project depedences. I used the "Package Manager Console" to add the 4 .packages
On page 5:
<div>In Chapter 7 section “Adding References” (page 5) you describe the commands that we must use if we want to add the required project dependencies, Ninject and Moq, using the Visual Studio Package Manager Console.You have something like this:
Install-Package Ninject -Project SportsStore.WebUI
Install-Package Ninject -Project SportsStore.Domain
Install-Package Moq -Project SportsStore.WebUI
Install-Package Moq -Project SportsStore.Domain
If you read the table 7-2, it seems that the info doesn’t match with the result of executing the previous commands.
Additionally, as a comment, the figure 7-15 (page 19 of the same Chapter) has a look & feel that doesn’t correspond with the rest of the images in the chapter.</div>
sbower1
Member
17 Points
9 Posts
Re: Errors on making mock repository
Mar 19, 2012 05:09 AM|LINK
I was stuck on the this same issue, and found that if you right click on SportsStore.Domain than select Rebuild it correct these errors. There is a problem when you build the entier solution with all three project which brings the errors back! I still don't know why this is happening.
I added a reference to Moq within SportsStore.WebUI, which corrected the error I was getting when trying to use the Mock<> method. According to the book SportsStores.UnitTests was the only project needing this reference.
sbower1
Member
17 Points
9 Posts
Re: Errors on making mock repository
Mar 19, 2012 05:56 AM|LINK
I found this on Stack Overflow that was not mentions in the text. Simply added a reference to the SportsStore.Domain in your SportsStore.WebUI, and then add all of these using statements. None of this was part of the steps in the book.
http://stackoverflow.com/questions/3554658/how-to-use-a-class-from-one-c-sharp-project-with-another-c-sharp-project
This has at least solved the problem with the AddBindings method on page 163.
Hope this helps!
wonjartran
Participant
907 Points
1214 Posts
Re: Errors on making mock repository
Mar 19, 2012 04:22 PM|LINK
Thanks. Adding those using statements solved the errors. Running the application
got the correct result of Figure 7-8 on P. 168.
imauld
Member
145 Points
85 Posts
Re: Errors on making mock repository
Mar 21, 2012 04:38 PM|LINK
A quick method to check if you're missing a using staement is to click on the piece of code causing the error and press "Ctrl + .' (That's Control and a period). Visual Studio will usually be able to find the using reference you're missing and add it it to the file.
sbower1
Member
17 Points
9 Posts
Re: Errors on making mock repository
Mar 23, 2012 04:27 AM|LINK
Thanks but I am aware of that Little trick and it is a nice feature. In my case the class methods were located in another project within the same solution. The designer does not give you the ability to CTL + . and find the correct reference it only ask if you need to create a new class. By added this using statement SportsStore.Domain; I gained access to the classes and methods within that namespace I was missing. I could have added it to the web.config namespace tag as a global reference that probably would have solved a lot of trouble. I'd never worked with multiple project in the same solution, and was not aware of that trick until later on.
Nickk1988
Member
2 Points
1 Post
Re: Errors on making mock repository
Jan 26, 2013 09:02 PM|LINK
I did that and i get Error with AsQueryable
Error 1 'System.Collections.Generic.List<SportsStore.Domain.Entities.Product>' does not contain a definition for 'AsQueryable' and no extension method 'AsQueryable' accepting a first argument of type 'System.Collections.Generic.List<SportsStore.Domain.Entities.Product>' could be found (are you missing a using directive or an assembly reference?) c:\users\nick\documents\visual studio 2012\Projects\SportsStore\SportsStore.WebUI\Infrastructure\NinjectControllerFactory.cs 35 3 SportsStore.WebUI
add using system.linq; and it will work