public void Setup()
		{

            subscription = new MailingListSubscription
            {
                Contact = new Contact()
            };

            var repository = new FakeRepository(id => subscription);
		    var repositoryResolver = MockRepository.GenerateStub<IRepositoryResolver>();
		    repositoryResolver.Stub(r => r.GetRepository(typeof (MailingListSubscription))).Return(repository);

			binder = new MailingListSubscriptionBinder(repositoryResolver);


			valueProvider = new FakeValueProvider();
			context = new ModelBindingContext()
			{
                ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(MailingListSubscription)),
				ModelState =  new ModelStateDictionary(),
				ValueProvider = valueProvider
			};

			controllerContext = new ControllerContext
			{
				HttpContext = MockRepository.GenerateStub<HttpContextBase>() 				
			};
			controllerContext.HttpContext.Expect(x => x.Request).Return(MockRepository.GenerateStub<HttpRequestBase>());
			controllerContext.HttpContext.Request.Expect(x => x.Form).Return(new NameValueCollection());
        }
		protected override object CreateInstance(System.Web.Mvc.ControllerContext controllerContext, System.Web.Mvc.ModelBindingContext bindingContext) 
		{
			var subscription = new MailingListSubscription
			{
				Contact = new Contact()
			};

			return subscription;
		}
		public void IndexWithPost_RedirectsOnSuccessfulBindingAndInsertsSubscription()
		{
			var subscription = new MailingListSubscription() { Email = "foo" };
			controller.Index(subscription, null)
				.ReturnsRedirectToRouteResult()
				.ToAction("Confirm");

				mailingListRepository.AssertWasCalled(x => x.InsertOnSubmit(subscription));
			
		}
		public void IndexWithPost_RendersViewOnFailedBinding()
		{
			controller.ModelState.AddModelError("foo", "bar");
			var subscription = new MailingListSubscription() { Email = "foo"};

			controller.Index(subscription, null)
				.ReturnsViewResult()
				.WithModel<ShopViewData>()
				.AssertAreEqual(subscription, x => x.MailingListSubscription);

		}
		void ValidateContact(ModelBindingContext bindingContext, ControllerContext controllerContext, MailingListSubscription subscription)
		{
			try
			{
				validatingBinder.UpdateFrom(subscription.Contact, controllerContext.HttpContext.Request.Form,
				                            bindingContext.ModelState, bindingContext.ModelName + ".Contact");
			}
			catch(ValidationException)
			{
				//Error details are stored in modelstate - the exception does not need to be propagated. 
			}
		}
		public void Delete_DeletesSubscription()
		{
			var subscription = new MailingListSubscription();
			mailingListRepository.Expect(x => x.GetById(5)).Return(subscription);
			controller.Delete(5)
				.ReturnsRedirectToRouteResult()
				.ToAction("List");
			mailingListRepository.AssertWasCalled(x => x.DeleteOnSubmit(subscription));
		}
		public void EditWithPost_RendersViewOnFailedBinding()
		{
			var subscription = new MailingListSubscription();
			controller.ModelState.AddModelError("foo", "bar");

				var countries = new List<Country>().AsQueryable();
			countryRepository.Expect(x => x.GetAll()).Return(countries);


			controller.Edit(subscription)
				.ReturnsViewResult()
				.WithModel<ShopViewData>()
				.AssertAreSame(countries, x => x.Countries)
				.AssertAreSame(subscription, x => x.MailingListSubscription);
		}
		public void EditWithPost_RedirectsOnSuccessfulBinding()
		{
			var subscription = new MailingListSubscription();
			controller.Edit(subscription)
				.ReturnsRedirectToRouteResult()
				.ToAction("List");
		}
		public void Edit_DisplaysSubscription()
		{
			var countries = new List<Country>().AsQueryable();
			countryRepository.Expect(x => x.GetAll()).Return(countries);

			var subscription = new MailingListSubscription();
			mailingListRepository.Expect(x => x.GetById(5)).Return(subscription);

			controller.Edit(5)
				.ReturnsViewResult()
				.WithModel<ShopViewData>()
				.AssertAreSame(countries, x => x.Countries)
				.AssertAreSame(subscription, x => x.MailingListSubscription);
		}
		public ActionResult Confirm([DataBind] Order order)
		{
			order.OrderStatusId = OrderStatus.CreatedId;

			if(order.ContactMe)
			{
				var mailingListSubscription = new MailingListSubscription
				{
					Contact = order.Contact1 ?? order.Contact,
					Email = order.Email
				};

				mailingListRepository.InsertOnSubmit(mailingListSubscription);
			}

			EmailOrder(order);
			userService.CurrentUser.CreateNewBasket();
			return this.RedirectToAction<OrderController>(c => c.Item(order.OrderId));
		}
		public ShopViewData WithSubscription(MailingListSubscription subscription)
		{
			this.MailingListSubscription = subscription;
			return this;
		}