【发布时间】:2021-07-25 20:21:43
【问题描述】:
作为评估,我收到了以下测试用例,因此我可以实现背后的代码:
[TestCase]
public void ProgrammerTest()
{
var address = new Address("56 Main St", "Mesa", "AZ", "38574");
var customer = new Customer("John", "Doe", address);
var company = new Company("Google", address);
Assert.IsNullOrEmpty(customer.Id);
customer.Save();
Assert.IsNotNullOrEmpty(customer.Id);
Assert.IsNullOrEmpty(company.Id);
company.Save();
Assert.IsNotNullOrEmpty(company.Id);
Customer savedCustomer = Customer.Find(customer.Id);
Assert.IsNotNull(savedCustomer);
Assert.AreSame(customer.Address, address);
Assert.AreEqual(savedCustomer.Address, address);
Assert.AreEqual(customer.Id, savedCustomer.Id);
Assert.AreEqual(customer.FirstName, savedCustomer.FirstName);
Assert.AreEqual(customer.LastName, savedCustomer.LastName);
Assert.AreEqual(customer, savedCustomer);
Assert.AreNotSame(customer, savedCustomer);
Company savedCompany = Company.Find(company.Id);
Assert.IsNotNull(savedCompany);
Assert.AreSame(company.Address, address);
Assert.AreEqual(savedCompany.Address, address);
Assert.AreEqual(company.Id, savedCompany.Id);
Assert.AreEqual(company.Name, savedCompany.Name);
Assert.AreEqual(company, savedCompany);
Assert.AreNotSame(company, savedCompany);
customer.Delete();
Assert.IsNullOrEmpty(customer.Id);
Assert.IsNull(Customer.Find(customer.Id));
company.Delete();
Assert.IsNullOrEmpty(company.Id);
Assert.IsNull(Company.Find(company.Id));
}
要求是:
- 在 C# 中创建一个单独的类,当子类化时允许此示例测试代码运行仅使用文件系统进行存储,不允许预建数据库;使用文件。
- 创建编译和通过测试用例所需的所有类;你不能修改测试。测试没有错,不是骗人的。
-
Id、Save、Delete和Find方法只能在超类中;子类不得自行实现这些方法。
这是我可以从测试用例中提取的一个类示例:
public abstract class MyBase
{
public abstract void Save();
public abstract void Delete();
}
public class Company : MyBase
{
public string Id { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
public Company(string name, Address address)
{
this.Name = name;
this.Address = address;
}
internal static Company Find(string id)
{
throw new NotImplementedException();
}
public override void Delete()
{
throw new NotImplementedException();
}
public override void Save()
{
object company = new List<Company>();
if (company != null)
{
// serialize JSON to a string and then write string to a file
File.WriteAllText(@"c:\company.json", JsonConvert.SerializeObject(company));
}
}
}
我有为给定代码创建 TestCases 的经验,但不是其他方式,我可以从中提取类,但是,我如何将方法 Save()、Delete()、Find(customer.Id) 实现为没有的通用类型传递填充的数据类?我知道我可以做类似void Save(object obj) 的事情并在Base 上实现传递的对象,但是测试用例只是像customer.Save() 这样调用......那么,我该怎么做呢?
提前感谢您的任何意见!
【问题讨论】: