【问题标题】:some of your test did a full page reload你的一些测试做了一个完整的页面重新加载
【发布时间】:2022-02-25 02:18:59
【问题描述】:

当我做角度单元测试时,发现了这个错误

警报:“添加成功!” Chrome 58.0.3029 (Windows 10 0.0.0) 错误 你的一些测试做了一个完整的页面重新加载! Chrome 58.0.3029(Windows 10 0.0.0):执行 0 of 1 错误(0.503 秒/0 秒)

// strategyAdd.component.ts
export class StrategyAdd{

	strategy = new Strategy();
  status:string;
  iscookies = ['','Yes','No'];
  isuseragents = ['','Yes','No'];
  constructor( private strategyService: StrategyTablesService,
  						 private router:Router,
  			   		 private location: Location
  ) { }


  onClickCreate(strategy:Strategy):void {
    strategy.starttime = this.getDate();
  	this.strategyService.createStrategy(strategy).subscribe((data) => {
  		this.status = data.json().status;
  		if(this.status=="succeed"){
  			alert("Add Success!");
  			location.reload();
  		}else{
  			alert("Add failed!");
  		}
  	},
  	error => console.log(error));
  }
}

//// strategyAdd.component.spec.ts
class MockStrategyTablesService extends StrategyTablesService{
  //noinspection JSAnnotator
  createStrategy(strategy:Strategy){
    var mockData={
      "strategyid" : "12",
      "status" : "succeed"
    }
    return Observable.of({
      json:() => mockData
    });
  }
}
describe('override provide Service',()=>{
  let comp;
  let strategy = new Strategy();
  beforeEach(()=>{
    TestBed.configureTestingModule({
      imports:[HttpModule,RouterTestingModule],
      providers:[
        StrategyAdd,
        {provide:StrategyTablesService,useClass:MockStrategyTablesService},
        //{provide:Router,useClass:RouterStub},
        Location,
      ]
    });

  });

  beforeEach(inject([StrategyAdd],s => {
    comp = s;
  }));

  it('test onClickCreate',async(()=>{
    comp.onClickCreate(strategy);
    expect(comp.status).toEqual("success");
  }));
});

【问题讨论】:

    标签: angular testing jasmine karma-runner


    【解决方案1】:

    您执行 location.reload() 并使用真实位置 - 所以它会重新加载页面。尝试从 providers 数组中移除 Location,因为 RouterTestingModule 已经提供了一个存根 Location。

    【讨论】:

      【解决方案2】:

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-03
      • 2017-04-25
      • 1970-01-01
      • 1970-01-01
      • 2013-08-22
      • 1970-01-01
      • 2016-11-17
      相关资源
      最近更新 更多