我在单元测试控制器类中调用任何函数时遇到问题,当我运行测试时,它给出错误
你调用的对象是空的
我不确定我在这里缺少什么,或者我不知道我在单元测试中调用函数,我正在使用 Visual Studio 2015 企业版的 Intellitest
这是我的代码
/// 此类包含 AccountFinFilesController 的参数化单元测试
[TestClass] [PexClass(typeof(AccountFinFilesController))] [PexAllowedExceptionFromTypeUnderTest(typeof(ArgumentException), AcceptExceptionSubtypes = true)] [PexAllowedExceptionFromTypeUnderTest(typeof(InvalidOperationException))] public partial class AccountFinFilesControllerTest { /// <summary>Test stub for .ctor(ISmallBizRepository)</summary> [PexMethod] public AccountFinFilesController ConstructorTest(ISmallBizRepository repo) { AccountFinFilesController target = new AccountFinFilesController(repo); return target; // TODO: add assertions to method AccountFinFilesControllerTest.ConstructorTest(ISmallBizRepository) } /// <summary>Test stub for Delete(Guid, Guid)</summary> [PexMethod] public HttpResponseMessage DeleteTest( [PexAssumeUnderTest]AccountFinFilesController target, Guid id, Guid deletedby ) { HttpResponseMessage result = target.Delete(id, deletedby); return result; // TODO: add assertions to method AccountFinFilesControllerTest.DeleteTest(AccountFinFilesController, Guid, Guid) } /// <summary>Test stub for GetAccountFinFilesTable(Guid, Guid)</summary> [PexMethod] public IQueryable GetAccountFinFilesTableTest( [PexAssumeUnderTest]AccountFinFilesController target, Guid firmid, Guid id ) { IQueryable result = target.GetAccountFinFilesTable(firmid, id); Assert.IsNotNull(result); return result; // TODO: add assertions to method AccountFinFilesControllerTest.GetAccountFinFilesTableTest(AccountFinFilesController, Guid, Guid) } /// <summary>Test stub for GetAccountFinFilesTable(Guid)</summary> [PexMethod] public IQueryable GetAccountFinFilesTableTest01([PexAssumeUnderTest]AccountFinFilesController target, Guid firmid) { IQueryable result = target.GetAccountFinFilesTable(Guid.Parse("75165ae3-cbae-e511-b0bf-00259076695a")); Assert.IsNotNull(result); return result; // TODO: add assertions to method AccountFinFilesControllerTest.GetAccountFinFilesTableTest01(AccountFinFilesController, Guid) } /// <summary>Test stub for Post(AccountFinFilesTable, Guid, Guid)</summary> [PexMethod] public HttpResponseMessage PostTest( [PexAssumeUnderTest]AccountFinFilesController target, AccountFinFilesTable obj, Guid firmid, Guid createdby ) { HttpResponseMessage result = target.Post(obj, firmid, createdby); return result; // TODO: add assertions to method AccountFinFilesControllerTest.PostTest(AccountFinFilesController, AccountFinFilesTable, Guid, Guid) } /// <summary>Test stub for Put(AccountFinFilesTable)</summary> [PexMethod] public HttpResponseMessage PutTest([PexAssumeUnderTest]AccountFinFilesController target, AccountFinFilesTable obj) { HttpResponseMessage result = target.Put(obj); return result; // TODO: add assertions to method AccountFinFilesControllerTest.PutTest(AccountFinFilesController, AccountFinFilesTable) } }
WebAPI 控制器类
public class AccountFinFilesController : BaseApiController { private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); public AccountFinFilesController(ISmallBizRepository repo) : base(repo) { } // GET api/AccountFinFiles /// <summary> /// GetAccountFinFilesTable /// </summary> /// <param name="firmid"></param> /// <param name="id"></param> /// <returns></returns> [HttpGet] public IQueryable GetAccountFinFilesTable(Guid firmid, Guid id) { log.Info("GetAccountFinFilesTable API -- firmid=" + firmid + "&id=" + id); return TheRepository.GetAccountFinFile(firmid, id); } /// <summary> /// GetAccountFinFilesTable /// </summary> /// <param name="firmid"></param> /// <returns></returns> [HttpGet] public IQueryable GetAccountFinFilesTable(Guid firmid) { log.Info("GetAccountFinFilesTable API -- firmid=" + firmid); return TheRepository.GetAccountFinFile(firmid); } /// <summary> /// update AccountFinFilesTable /// </summary> /// <param name="account_finfilestabledm"></param> /// <returns></returns> [HttpPut] public HttpResponseMessage Put(AccountFinFilesTable obj) { try { var updatedEntity = TheModelFactory.Parse(obj); if (updatedEntity == null) { log.Error(MessagesHelper.recordnotfound + "File_ID: " + obj.File_ID); Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not read AccountFinFilesTable from body"); } var originalEntity = TheRepository.GetAccountFinFileByPrimaryKey(obj.File_ID); if (originalEntity == null || originalEntity.File_ID != obj.File_ID) { log.Error(MessagesHelper.recordnotfound + "File_ID: " + obj.File_ID); return Request.CreateResponse(HttpStatusCode.NotModified, "AccountFinFilesTable record not found"); } else { updatedEntity.File_ID = obj.File_ID; } if (TheRepository.Update(originalEntity, updatedEntity) && TheRepository.SaveAll()) { log.Info(MessagesHelper.updatesuccess + "File_ID: " + obj.File_ID); return Request.CreateResponse(HttpStatusCode.OK, TheModelFactory.Create(updatedEntity)); } else { log.Error(MessagesHelper.updateerror + "File_ID: " + obj.File_ID); return Request.CreateResponse(HttpStatusCode.NotModified, "Could not update to the database."); } } catch (Exception ex) { log.Error(ex.Message + " " + obj.File_ID); return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex); } } /// <summary> /// add new record to AccountFinFilesTable /// </summary> /// <param name="account_finfilestabledm"></param> /// <param name="firmid"></param> /// <param name="createdby"></param> /// <returns></returns> [HttpPost] public HttpResponseMessage Post(AccountFinFilesTable obj, Guid firmid, Guid createdby) { try { obj.FirmId = firmid; obj.ClientId = createdby; obj.CreatedDate = DateTime.UtcNow; var entity = TheModelFactory.Parse(obj); if (entity == null) { log.Error(MessagesHelper.recordnotfound + " " + firmid + " " + createdby); return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not read AccountFinFilesTable from body"); } else { if (TheRepository.Insert(entity) && TheRepository.SaveAll()) { log.Info(MessagesHelper.savesuccess + " " + firmid + " " + createdby); return Request.CreateResponse(HttpStatusCode.Created, TheModelFactory.Create(entity)); } else { log.Error(MessagesHelper.saverror + " " + firmid + " " + createdby); return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not save to the database."); } } } catch (Exception ex) { log.Error(ex.Message + " " + firmid + " " + createdby); return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex); } } /// <summary> /// delete record from AccountFinFilesTable /// </summary> /// <param name="account_finfilestabledm"></param> /// <param name="deletedby"></param> /// <returns></returns> [HttpDelete] public HttpResponseMessage Delete(Guid id, Guid deletedby) { try { var entity = TheRepository.GetAccountFinFileByPrimaryKey(id); if (entity == null) { log.Error(MessagesHelper.recordnotfound + "File_ID: " + id); return Request.CreateResponse(HttpStatusCode.NotFound); } if (TheRepository.DeleteAccountFinFile(id, deletedby) && TheRepository.SaveAll()) { log.Info(MessagesHelper.deletesuccess + "File_ID: " + entity.File_ID); return Request.CreateResponse(HttpStatusCode.OK); } else { log.Error(MessagesHelper.deleteerror + "File_ID: " + entity.File_ID); return Request.CreateResponse(HttpStatusCode.BadRequest, "Could not delete the record."); } } catch (Exception ex) { log.Error(ex.Message + "File_ID: " + id); return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message); } } }
错误
请根据我的情况帮助/指导或为我提供完整的解决方案,如果需要,我将提供更多详细信息。感谢您宝贵的时间和精力。