我有一个 Jasmine 测试,其中包含一些 HTTP 请求。我正在使用 Axios 进行基于承诺的 HTTP 访问,但由于某种原因我得到了axios is not defined. 我已经跑了npm install axios --save
var request = require('axios');
var constants = require('../../lib/constants.js');
describe('Signing into the application', function () {
it('returns 200 OK', function () {
axios.get(constants.Endpoint)
.then(function (response) {
console.log(JSON.stringify(response));
})
.catch(function (error) {
console.log(JSON.stringify(error));
});
});
});
Run Code Online (Sandbox Code Playgroud)
这是输出:
Failures:
1) Signing into the application returns 200 OK
Message:
ReferenceError: axios is not defined
Stacktrace:
ReferenceError: axios is not defined
at jasmine.Spec.<anonymous> (C:\Users\la\Documents\tests\spec\integration\sign-in\sign-in-spec.js:6:9)
Run Code Online (Sandbox Code Playgroud)
这里是package.json …
如果我在组件中有以下删除方法
@Component({
selector: 'app-heroes',
templateUrl: './heroes.component.html',
styleUrls: ['./heroes.component.css']
})
export class HeroesComponent {
this.heroes = [];
constructor(private heroService: HeroService) { }
delete(hero: Hero): void {
this.heroes = this.heroes.filter(h => h !== hero);
this.heroService.deleteHero(hero).subscribe();
}
}
Run Code Online (Sandbox Code Playgroud)
我如何在 Jasmine 中测试删除英雄方法在测试中调用了订阅。
这是一个测试,确保使用特定参数调用 deleteHero,但我不确定如何检查订阅。
// Interaction Test
it('should call deleteHero with the specificed argument', () => {
// arrange
mockHeroService.deleteHero.and.returnValue(of(true));
component.heroes = HEROES;
// act
component.delete(HEROES[2]);
// assert
expect(mockHeroService.deleteHero).toHaveBeenCalledWith(HEROES[2]);
});
Run Code Online (Sandbox Code Playgroud) 我创建了一个 Injectable,其中包含要使用的同一文件中的全局变量的声明。我能够让它在代码中工作。但在我的测试中,声明失败并出现未定义的错误。
declare var myGlobal;
@Injectable()
export class HttpService {
constructor() {
console.log(myGlobal);
}
}
Run Code Online (Sandbox Code Playgroud)
我正在测试一个组件,并且需要此服务作为组件测试的测试台中的提供者。
以下是它的调用方式:
@Component({
...
})
export class AppComponent {
constructor(_h: HttpService) {
}
ngOnInit(): void {
this._h.fileUrl = window.location.href;
this.getSettings(this._h.settingsSrc);
}
}
Run Code Online (Sandbox Code Playgroud)
以下是测试中服务的声明
beforeEach(async(() => {
const settingsFile = 'json';
TestBed.configureTestingModule({
declarations: [
AppComponent,
MenubarComponent,
],
imports: [
HttpClientModule,
],
providers: [
HttpService
],
}).compileComponents();}))
it('getSettings() tests', inject([HttpService], async(async (_h: HttpService) => {
const cmp = new AppComponent(_h);
await cmp.ngOnInit(); // this is where the …Run Code Online (Sandbox Code Playgroud) 在我的组件内部,ngOnInit我有一个subscribe()内部调用组件函数:
ngOnInit(): void {
this.navigationService.itemReset$.subscribe(event => this.resetItem(event));
}
Run Code Online (Sandbox Code Playgroud)
在导航服务中我有:
btnPressed$: Subject<Event> = new Subject();
itemReset$: Observable<Event> = this.btnPressed$.asObservable();
keyDown(event: Event): void {
this.btnPressed$.next(event);
}
Run Code Online (Sandbox Code Playgroud)
我搜索了很多关于这个问题的信息,并在我的组件测试中尝试了类似的方法:
ngOnInit(): void {
this.navigationService.itemReset$.subscribe(event => this.resetItem(event));
}
Run Code Online (Sandbox Code Playgroud)
但仍然出现错误
TypeError: this.navigationService.itemReset$.subscribe is not a function
Run Code Online (Sandbox Code Playgroud)
知道吗,为什么它不起作用?我将非常感谢任何帮助!
我一直在学习 CoffeeScript,作为学习它的练习,我决定学习 TDD Conway 的 Game of Life。我选择的第一个测试是创建一个 Cell 并查看它是死还是活。为此,我创建了以下咖啡脚本:
class Cell
@isAlive = false
constructor: (isAlive) ->
@isAlive = isAlive
die: ->
@isAlive = false
Run Code Online (Sandbox Code Playgroud)
然后,我使用以下代码创建一个 Jasmine 测试文件(这是故意失败的测试):
Cell = require '../conway'
describe 'conway', ->
alive = Cell.isAlive
cell = null
beforeEach ->
cell = new Cell()
describe '#die', ->
it 'kills cell', ->
expect(cell.isAlive).toBeTruthy()
Run Code Online (Sandbox Code Playgroud)
但是,当我在 Jasmine 中运行测试时,出现以下错误:
cell is not defined
Run Code Online (Sandbox Code Playgroud)
和堆栈跟踪:
1) kills cell
Message:
ReferenceError: cell is not defined
Stacktrace:
ReferenceError: cell is not defined …Run Code Online (Sandbox Code Playgroud) jasmine ×5
angular ×3
javascript ×3
rxjs ×2
axios ×1
coffeescript ×1
components ×1
karma-runner ×1
node.js ×1
testing ×1