Angular 2模拟方法和调用

时间:2016-11-09 17:42:23

标签: testing angular jasmine

我正在尝试调用模拟服务并更新用户变量,但是当我调用模拟服务时没有任何变化?

我一般都在努力进行测试,如果有任何好的资源可以用angular2学习,我会全力以赴。

let users = [{ name: 'Test', lastname: 'User' },  { name: 'Test2', lastname: 'User2' }];
let addedUsers = [{ name: 'blah', lastname: 'blah' },{ name: 'Test', lastname: 'User' },  { name: 'Test2', lastname: 'User2' }];

describe('Component: UserList', () => {
 beforeEach(() => {

 userServiceStub = {
  getUsers: () => {
    return Observable.of(users);
  },
  getUser: () => {
    return Observable.of(user);
  },
  addUser: () => {
    users.push({ name: 'blah', lastname: 'blah' });       
  }
};

TestBed.configureTestingModule({
  declarations: [UserListComponent],
  imports: [HttpModule],
  providers: [ 
      {provide: UsersService, useValue: userServiceStub },
      { provide: Router, useClass: RouterStub } 
    ]
});

app = fixture.debugElement.componentInstance; 
userService = fixture.debugElement.injector.get(UsersService);

it('should call addUser on button click', () => {
 let spy = spyOn(userService, 'addUser');     
 userService.addUser('argument'); 
 fixture.detectChanges();     
 expect(users).toEqual(addedUsers);
});

});

0 个答案:

没有答案