Commits

Eli Weinstock-Herman committed 82d254c

Test project added, abstracted DbContext to prepare for tests, forgot admin password

Comments (0)

Files changed (19)

Local.testsettings

+<?xml version="1.0" encoding="UTF-8"?>
+<TestSettings name="Local" id="140c0233-312b-4d63-812e-6732466be1d4" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
+  <Description>These are default test settings for a local test run.</Description>
+  <Deployment enabled="false" />
+  <Execution>
+    <TestTypeSpecific />
+    <AgentRule name="Execution Agents">
+    </AgentRule>
+  </Execution>
+</TestSettings>

MvcMusicStore.sln

 # Visual Studio 2010
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MvcMusicStore", "MvcMusicStore\MvcMusicStore.csproj", "{C9390804-FF00-4AE4-A70F-F9BF7B5C2655}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MvcMusicStoreTests", "MvcMusicStoreTests\MvcMusicStoreTests.csproj", "{3144E517-5D54-4242-B630-176B6F0212F9}"
+EndProject
+Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{DB4CE3A6-7BED-4A18-BF8E-D4DD46977756}"
+	ProjectSection(SolutionItems) = preProject
+		Local.testsettings = Local.testsettings
+		MvcMusicStore.vsmdi = MvcMusicStore.vsmdi
+		TraceAndTestImpact.testsettings = TraceAndTestImpact.testsettings
+	EndProjectSection
+EndProject
 Global
+	GlobalSection(TestCaseManagementSettings) = postSolution
+		CategoryFile = MvcMusicStore.vsmdi
+	EndGlobalSection
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
 		Release|Any CPU = Release|Any CPU
 		{C9390804-FF00-4AE4-A70F-F9BF7B5C2655}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{C9390804-FF00-4AE4-A70F-F9BF7B5C2655}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{C9390804-FF00-4AE4-A70F-F9BF7B5C2655}.Release|Any CPU.Build.0 = Release|Any CPU
+		{3144E517-5D54-4242-B630-176B6F0212F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{3144E517-5D54-4242-B630-176B6F0212F9}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{3144E517-5D54-4242-B630-176B6F0212F9}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{3144E517-5D54-4242-B630-176B6F0212F9}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

MvcMusicStore/App_Data/Membership.sdf

Binary file modified.

MvcMusicStore/App_Data/MvcMusicStore.sdf

Binary file modified.

MvcMusicStore/Controllers/AccountController.cs

 namespace MvcMusicStore.Controllers {
 	public class AccountController : Controller {
 
+
+
 		//
 		// GET: /Account/LogOn
 

MvcMusicStore/Controllers/CheckoutController.cs

 namespace MvcMusicStore.Controllers {
 
 	[Authorize]
-	public class CheckoutController : Controller {
-		MusicStoreEntities storeDB = new MusicStoreEntities();
+	public class CheckoutController : ControllerBase {
+		
 		const string PromoCode = "FREE";
 
 		//
 					order.OrderDate = DateTime.Now;
 
 					//Save Order
-					storeDB.Orders.Add(order);
-					storeDB.SaveChanges();
+					StoreDB.Orders.Add(order);
+					StoreDB.SaveChanges();
 					//Process the order
 					var cart = ShoppingCart.GetCart(this.HttpContext);
 					cart.CreateOrder(order);
 		// GET: /Checkout/Complete
 		public ActionResult Complete(int id) {
 			// Validate customer owns this order
-			bool isValid = storeDB.Orders.Any(o => o.OrderId == id && o.Username == User.Identity.Name);
+			bool isValid = StoreDB.Orders.Any(o => o.OrderId == id && o.Username == User.Identity.Name);
 
 			if (isValid) {
 				return View(id);

MvcMusicStore/Controllers/ControllerBase.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Web;
+using System.Web.Mvc;
+using MvcMusicStore.Models;
+
+namespace MvcMusicStore.Controllers {
+	public class ControllerBase : Controller {
+
+		private IMusicStoreEntities _storeDB;
+
+		protected IMusicStoreEntities StoreDB { get { return _storeDB; } }
+
+		public ControllerBase() : this(new MusicStoreEntities()) { }
+
+		public ControllerBase(IMusicStoreEntities storeDb) {
+			_storeDB = storeDb;
+		}
+
+	}
+}

MvcMusicStore/Controllers/HomeController.cs

 using MvcMusicStore.Models;
 
 namespace MvcMusicStore.Controllers {
-	public class HomeController : Controller {
+	public class HomeController : ControllerBase {
 		//
 		// GET: /Home/
-		MusicStoreEntities storeDB = new MusicStoreEntities();
+		
 		public ActionResult Index() {
 			// Get most popular albums
 			var albums = GetTopSellingAlbums(5);
-
+			
 			return View(albums);
 		}
 		private List<Album> GetTopSellingAlbums(int count) {
 			// Group the order details by album and return
 			// the albums with the highest count
-			return storeDB.Albums
+			return StoreDB.Albums
 				.OrderByDescending(a => a.OrderDetails.Count())
 				.Take(count)
 				.ToList();

MvcMusicStore/Controllers/ShoppingCartController.cs

 using MvcMusicStore.ViewModels;
 
 namespace MvcMusicStore.Controllers {
-	public class ShoppingCartController : Controller {
-		MusicStoreEntities storeDB = new MusicStoreEntities();
+	public class ShoppingCartController : ControllerBase {
+		
 		//
 		// GET: /ShoppingCart/
 		public ActionResult Index() {
 		// GET: /Store/AddToCart/5
 		public ActionResult AddToCart(int id) {
 			// Retrieve the album from the database
-			var addedAlbum = storeDB.Albums
+			var addedAlbum = StoreDB.Albums
 				.Single(album => album.AlbumId == id);
 
 			// Add it to the shopping cart
 			var cart = ShoppingCart.GetCart(this.HttpContext);
 
 			// Get the name of the album to display confirmation
-			string albumName = storeDB.Carts
+			string albumName = StoreDB.Carts
 				.Single(item => item.RecordId == id).Album.Title;
 
 			// Remove from cart

MvcMusicStore/Controllers/StoreController.cs

 using System.Web;
 using System.Web.Mvc;
 using MvcMusicStore.Models;
+using System.Data.Entity;
 
 namespace MvcMusicStore.Controllers {
-	public class StoreController : Controller {
-		MusicStoreEntities storeDB = new MusicStoreEntities();
+	public class StoreController : ControllerBase {
 
 		//
 		// GET: /Home/
 		public ActionResult Index() {
-			var genres = storeDB.Genres.ToList();
+			var genres = StoreDB.Genres.ToList();
 			return View(genres);
 
 		}
 		//
 		// GET: /Store/Browse?genre=Disco
 		public ActionResult Browse(string genre) {
-			var genreModel = storeDB.Genres.Include("Albums").Single(g => g.Name == genre);
+			var genreModel = StoreDB.Genres.Include("Albums").Single(g => g.Name == genre);
 			return View(genreModel);
 		}
 
 		//
 		// GET: /Store/Details/5
 		public ActionResult Details(int id) {
-			var album = storeDB.Albums.Find(id);
+			var album = StoreDB.Albums.Find(id);
 			return View(album);
 
 		}
 		// GET: /Store/GenreMenu
 		[ChildActionOnly]
 		public ActionResult GenreMenu() {
-			var genres = storeDB.Genres.ToList();
+			var genres = StoreDB.Genres.ToList();
 			return PartialView(genres);
 		}
 	}

MvcMusicStore/Controllers/StoreManagerController.cs

 namespace MvcMusicStore.Controllers
 {
 	[Authorize(Roles = "Administrator")]
-	public class StoreManagerController : Controller
+	public class StoreManagerController : ControllerBase
 	{
-		private MusicStoreEntities db = new MusicStoreEntities();
-
+		
 		//
 		// GET: /StoreManager/
 
 		public ViewResult Index()
 		{
-			var albums = db.Albums.Include(a => a.Genre).Include(a => a.Artist);
+			var albums = StoreDB.Albums.Include(a => a.Genre).Include(a => a.Artist);
 			return View(albums.ToList());
 		}
 
 
 		public ViewResult Details(int id)
 		{
-			Album album = db.Albums.Find(id);
+			Album album = StoreDB.Albums.Find(id);
 			return View(album);
 		}
 
 
 		public ActionResult Create()
 		{
-			ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name");
-			ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name");
+			ViewBag.GenreId = new SelectList(StoreDB.Genres, "GenreId", "Name");
+			ViewBag.ArtistId = new SelectList(StoreDB.Artists, "ArtistId", "Name");
 			return View();
 		} 
 
 		{
 			if (ModelState.IsValid)
 			{
-				db.Albums.Add(album);
-				db.SaveChanges();
+				StoreDB.Albums.Add(album);
+				StoreDB.SaveChanges();
 				return RedirectToAction("Index");  
 			}
 
-			ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
-			ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
+			ViewBag.GenreId = new SelectList(StoreDB.Genres, "GenreId", "Name", album.GenreId);
+			ViewBag.ArtistId = new SelectList(StoreDB.Artists, "ArtistId", "Name", album.ArtistId);
 			return View(album);
 		}
 		
  
 		public ActionResult Edit(int id)
 		{
-			Album album = db.Albums.Find(id);
-			ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
-			ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
+			Album album = StoreDB.Albums.Find(id);
+			ViewBag.GenreId = new SelectList(StoreDB.Genres, "GenreId", "Name", album.GenreId);
+			ViewBag.ArtistId = new SelectList(StoreDB.Artists, "ArtistId", "Name", album.ArtistId);
 			return View(album);
 		}
 
 		{
 			if (ModelState.IsValid)
 			{
-				db.Entry(album).State = EntityState.Modified;
-				db.SaveChanges();
+				StoreDB.SetModified(album);
+				StoreDB.SaveChanges();
 				return RedirectToAction("Index");
 			}
-			ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
-			ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
+			ViewBag.GenreId = new SelectList(StoreDB.Genres, "GenreId", "Name", album.GenreId);
+			ViewBag.ArtistId = new SelectList(StoreDB.Artists, "ArtistId", "Name", album.ArtistId);
 			return View(album);
 		}
 
  
 		public ActionResult Delete(int id)
 		{
-			Album album = db.Albums.Find(id);
+			Album album = StoreDB.Albums.Find(id);
 			return View(album);
 		}
 
 
 		[HttpPost, ActionName("Delete")]
 		public ActionResult DeleteConfirmed(int id)
-		{            
-			Album album = db.Albums.Find(id);
-			db.Albums.Remove(album);
-			db.SaveChanges();
+		{
+			Album album = StoreDB.Albums.Find(id);
+			StoreDB.Albums.Remove(album);
+			StoreDB.SaveChanges();
 			return RedirectToAction("Index");
 		}
 
 		protected override void Dispose(bool disposing)
 		{
-			db.Dispose();
+			StoreDB.Dispose();
 			base.Dispose(disposing);
 		}
 	}

MvcMusicStore/Models/MusicStoreEntities.cs

 using System.Linq;
 using System.Web;
 using System.Data.Entity;
+using System.Data;
 
 namespace MvcMusicStore.Models {
-	public class MusicStoreEntities : DbContext {
-		public DbSet<Album> Albums { get; set; }
-		public DbSet<Genre> Genres { get; set; }
+	public class MusicStoreEntities : DbContext, IMusicStoreEntities {
+		public IDbSet<Album> Albums { get; set; }
+		public IDbSet<Genre> Genres { get; set; }
 
-		public DbSet<Artist> Artists { get; set; }
+		public IDbSet<Artist> Artists { get; set; }
 
-		public DbSet<Cart> Carts { get; set; }
-		public DbSet<Order> Orders { get; set; }
-		public DbSet<OrderDetail> OrderDetails { get; set; }
+		public IDbSet<Cart> Carts { get; set; }
+		public IDbSet<Order> Orders { get; set; }
+		public IDbSet<OrderDetail> OrderDetails { get; set; }
 
+		public void SetModified(object target) {
+			Entry(target).State = EntityState.Modified;
+		}
+	}
+
+	public interface IMusicStoreEntities : IDisposable {
+		IDbSet<Album> Albums { get; set; }
+		IDbSet<Genre> Genres { get; set; }
+
+		IDbSet<Artist> Artists { get; set; }
+
+		IDbSet<Cart> Carts { get; set; }
+		IDbSet<Order> Orders { get; set; }
+		IDbSet<OrderDetail> OrderDetails { get; set; }
+
+		int SaveChanges();
+		void SetModified(object target);
 	}
 
 }

MvcMusicStore/MvcMusicStore.csproj

   <ItemGroup>
     <Compile Include="Controllers\AccountController.cs" />
     <Compile Include="Controllers\CheckoutController.cs" />
+    <Compile Include="Controllers\ControllerBase.cs" />
     <Compile Include="Controllers\HomeController.cs" />
     <Compile Include="Controllers\ShoppingCartController.cs" />
     <Compile Include="Controllers\StoreController.cs" />
   <ItemGroup>
     <Content Include="Views\Store\GenreMenu.cshtml" />
   </ItemGroup>
+  <ItemGroup />
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 

MvcMusicStoreTests/MvcMusicStoreTests.csproj

+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProductVersion>
+    </ProductVersion>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{3144E517-5D54-4242-B630-176B6F0212F9}</ProjectGuid>
+    <OutputType>Library</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>MvcMusicStoreTests</RootNamespace>
+    <AssemblyName>MvcMusicStoreTests</AssemblyName>
+    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+    <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
+    <Reference Include="Rhino.Mocks">
+      <HintPath>..\packages\RhinoMocks.3.6\lib\Rhino.Mocks.dll</HintPath>
+    </Reference>
+    <Reference Include="System" />
+    <Reference Include="System.Core">
+      <RequiredTargetFramework>3.5</RequiredTargetFramework>
+    </Reference>
+  </ItemGroup>
+  <ItemGroup>
+    <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+      <Visible>False</Visible>
+    </CodeAnalysisDependentAssemblyPaths>
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Properties\AssemblyInfo.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="..\MvcMusicStore\MvcMusicStore.csproj">
+      <Project>{C9390804-FF00-4AE4-A70F-F9BF7B5C2655}</Project>
+      <Name>MvcMusicStore</Name>
+    </ProjectReference>
+  </ItemGroup>
+  <ItemGroup>
+    <None Include="packages.config" />
+  </ItemGroup>
+  <ItemGroup>
+    <Folder Include="Controllers\" />
+  </ItemGroup>
+  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
+       Other similar extension points exist, see Microsoft.Common.targets.
+  <Target Name="BeforeBuild">
+  </Target>
+  <Target Name="AfterBuild">
+  </Target>
+  -->
+</Project>

MvcMusicStoreTests/Properties/AssemblyInfo.cs

+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following 
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("MvcMusicStoreTests")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("Microsoft")]
+[assembly: AssemblyProduct("MvcMusicStoreTests")]
+[assembly: AssemblyCopyright("Copyright © Microsoft 2011")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible 
+// to COM components.  If you need to access a type in this assembly from 
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("86f6bef0-5e22-4b3e-85ed-a877caf28d0b")]
+
+// Version information for an assembly consists of the following four values:
+//
+//      Major Version
+//      Minor Version 
+//      Build Number
+//      Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers 
+// by using the '*' as shown below:
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]

MvcMusicStoreTests/packages.config

+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+  <package id="RhinoMocks" version="3.6" />
+</packages>

TraceAndTestImpact.testsettings

+<?xml version="1.0" encoding="UTF-8"?>
+<TestSettings name="Trace and Test Impact" id="78be5325-f019-4b10-9fd4-7ca14d11acc7" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
+  <Description>These are test settings for Trace and Test Impact.</Description>
+  <Execution>
+    <TestTypeSpecific />
+    <AgentRule name="Execution Agents">
+    </AgentRule>
+  </Execution>
+</TestSettings>

packages/RhinoMocks.3.6/lib/Rhino.Mocks.dll

Binary file added.

packages/repositories.config

 <?xml version="1.0" encoding="utf-8"?>
 <repositories>
   <repository path="..\MvcMusicStore\packages.config" />
+  <repository path="..\MvcMusicStoreTests\packages.config" />
 </repositories>