3 articles .NET Unit Test

All article related with .NET unit test, regardless of library

ASP MVC Unit Test ModelState Always Valid

[code language=”csharp”] ProductModelEdit model = new ProductModelEdit() ; //Init ModelState var modelBinder = new ModelBindingContext() { ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType( () => model, model.GetType()), ValueProvider=new NameValueCollectionValueProvider( new NameValueCollection(), CultureInfo.InvariantCulture) }; var binder=new DefaultModelBinder().BindModel( new ControllerContext(),modelBinder ); ProductController.ModelState.Clear(); ProductController.ModelState.Merge(modelBinder.ModelState); ViewResult result = (ViewResult)ProductController.CreateProduct(null,model); Assert.IsTrue(result.ViewData.ModelState["Name"].Errors.Count > 0); Assert.True(result.ViewData.ModelState["Description"].Errors.Count > 0); Assert.True(!result.ViewData.ModelState.IsValid); [/code] http://stackoverflow.com/questions/286124/how-can-i-test-modelstate

ASP MVC Unit Test With Authorize

[code language=”csharp”] var controller = new UserController(); var mock = new Mock<ControllerContext>(); mock.SetupGet(x => x.HttpContext.User.Identity.Name).Returns("SOMEUSER"); mock.SetupGet(x => x.HttpContext.Request.IsAuthenticated).Returns(true); controller.ControllerContext = mock.Object; [/code] http://stackoverflow.com/questions/1877225/how-do-i-unit-test-a-controller-method-that-has-the-authorize-attribute-applie

ASP MVC Unit Test Not Detected

Please add the keyword public to your class definition. Your test class is currently not visible outside its own assembly. [code language=”csharp”] namespace tests { [TestClass] public class SimpleTest { [TestMethod] public void Test() { Assert.AreEqual("a","a", "same"); } } } [/code] http://stackoverflow.com/questions/13533259/why-does-visual-studio-2012-not-find-my-tests