/// <summary> /// Generates the test class for a table. /// </summary> /// <param name="writer">The writer to which the code is written.</param> /// <param name="namespaceName">The namespace containing the test class.</param> /// <param name="table">The table being tested.</param> public void Generate(TextWriter writer, string namespaceName, TableSchema table) { if (table == null) throw new ArgumentNullException("table"); NamespaceDeclaration nstest = new NamespaceDeclaration(namespaceName + ".Tests"); //declare the test class string typeName = this.NameProvider.GetClassName(table.Name); ClassDeclaration testclass = nstest.AddClass(typeName + "Tests", true); string targetTypeName = namespaceName + "." + typeName; testclass.InheritsFrom(new ClassDeclaration("ActiveRecordBaseTest", new CodeDomTypeReference(targetTypeName))); //create the test method MethodDeclaration testmeth = testclass.AddMethod(String.Format("Verify{0}", typeName)).Public(); testmeth.AddAttribute("NUnit.Framework.TestAttribute"); string fullName = namespaceName + "." + typeName; string fieldName = this.NameProvider.GetClassName(table.Name).Substring(0, 1).ToLower() + this.NameProvider.GetClassName(table.Name).Substring(1); testmeth.Declare(fieldName).New(new CodeDomTypeReference(fullName));//generates: Customer customer = new Customer(); foreach (ColumnSchema column in table.NonKeyColumns) { string propertyName = this.NameProvider.GetPropertyName(column); string propertyValue = this.ValueProvider.GetValue(column); testmeth.Assign(fieldName).Property(propertyName).To(propertyValue); } testmeth.Call(fieldName, "Save"); nstest.Imports("System"); CodeBuilder builder = new CodeBuilder(); builder.GenerateCode(writer, nstest); }