@isTest private class searchAgencyHospitalControllerTest { @isTest static void itShould(){ List rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '販売店']; if (rectCo.size() == 0) { return; } List rectHosp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院']; if (rectCo.size() == 0) { return; } Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证']; Account myAccount2 = new Account(name='Testaccount002',Dealer_discount__c =20,RecordTypeId = rectCo[0].Id,Product_Limit_Date__c = 'Test01|5|55,Test02|2|4'); insert myAccount2; // 病院 Account hospital1 = new Account(); hospital1.RecordTypeId = rectHosp[0].Id; hospital1.Name = 'TestHospital1'; upsert hospital1; Account hospital2 = new Account(); hospital2.RecordTypeId = rectHosp[0].Id; hospital2.Name = 'TestHospital2'; upsert hospital2; //经销商医院数据 Agency_Hospital_Link__c ahl = new Agency_Hospital_Link__c(); ahl.Agency__c = myAccount2.Id; ahl.Hospital__c = hospital1.Id; ahl.AgencyHos_ET__c = true; Agency_Hospital_Link__c ahl2 = new Agency_Hospital_Link__c(); ahl2.Agency__c = myAccount2.Id; ahl2.Hospital__c = hospital2.Id; ahl2.AgencyHos_ET__c = true; insert new Agency_Hospital_Link__c[]{ahl,ahl2}; Contact core = new Contact(email='jplumber@salesforce.com', firstname='Joe',lastname='Plumber',accountid=myAccount2.id); insert core; user MyUser_Test = New User(ContactId = core.id,Alias = 'newUser',Email='newuser@testorg.com',EmailEncodingKey='UTF-8', LastName='TestUser', LanguageLocaleKey='zh_CN',LocaleSidKey='zh_CN', ProfileId = prof.Id,TimeZoneSidKey='Asia/Shanghai', UserName='testUser@testorg.com'); insert MyUser_Test; system.runAs(MyUser_Test){ PageReference page = new PageReference('/apex/searchAgencyHospital?ctype=ET'); page.setRedirect(true); System.Test.setCurrentPage(page); searchAgencyHospitalController conTest = new searchAgencyHospitalController(); conTest.init(); System.assertEquals(2, conTest.at.size()); conTest.searchName = 'TestHospital1'; conTest.serContact(); System.assertEquals(1, conTest.at.size()); } } }