Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay, so i tried out unit testing and gave it a good shot, and i hated it...</p> <p>I needed to use the HttpContext and it was a pain in the ass to make that work when unit testing. So i wrote my own testing environment in MVC, it's quite simple:</p> <p>Your put on a attribute on the method you want testet, you build you project, and you refresh a page h**p://localhost/test to see the output of that method.</p> <pre><code>[QTest] public string test() { return HttpContext.Current.Server.MapPath(@"~\"); } </code></pre> <hr> <p>If anyone should be interested, here is the howto:</p> <ol> <li><p>Create a custom attribute. </p></li> <li><p>Create a test page, and find all methods in your assembly with an attribute QTest. </p></li> <li><p>Print them out.</p></li> </ol> <p>My code:</p> <p>CustomAttribute.cs</p> <pre><code>public class QTestAttribute : Attribute { public QTestAttribute() { //Will do so you can define the method paremeter values later on. But for now a emty attribute is fine. } } </code></pre> <p>Your TestController.cs:</p> <pre><code>static IEnumerable&lt;Type&gt; GetTypesWithAttribute(Assembly assembly) { foreach (Type type in assembly.GetTypes()) { yield return type; } } static IEnumerable&lt;MethodInfo&gt; GetMethodsWithAttribute(Type theClass) { foreach (MethodInfo method in theClass.GetMethods()) { if (method.GetCustomAttributes(typeof(QTestAttribute), true).Length &gt; 0) { yield return method; } } } public ActionResult Index() { IEnumerable&lt;Type&gt; classes = GetTypesWithAttribute(Assembly.LoadFrom(HttpContext.Request.MapPath(@"~\bin\YourProjectName.dll"))); List&lt;String&gt; tests = new List&lt;string&gt;(); foreach (var singleClass in classes) { try { var a = System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(singleClass.FullName); foreach (MethodInfo method in GetMethodsWithAttribute(singleClass)) { tests.Add(method.Invoke(a, null).ToString()); } } catch (Exception ex) { try { tests.Add(ex.InnerException.Message); } catch (Exception) { } } } return Content(string.Join("&lt;br&gt;", tests)); } </code></pre> <p>It's pretty easy to customize, and make it work how you want to, and it's without limitations compared to nunit.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload