我正在测试我的代码操作:
export function fetchAlunos() {
var itens = [];
return firebaseDatabase
.ref('/alunos')
.on('value', snap => {
snap.forEach(snapChild => {
var item = snapChild.val();
item.key = snapChild.key;
itens.push(item);
})
dispatch(fetchAlunoFinished(itens));
}, erro => dispatch(fetchAlunoError()))
}
export function addAluno(aluno) {
try{
if (aluno && !aluno.id) {
firebaseDatabase
.ref('/alunos')
.push(aluno)
} else {
var id = aluno.id;
delete aluno.id
firebaseDatabase
.ref()
.child('/alunos/' + id)
.set(aluno)
}
} catch (erro) {
console.log(erro);
}
return fetchAlunos();
}
测试是:
import 'firebase/database';
import thunk from 'redux-thunk'
import * as actions from './../../src/actions/aluno';
import * as types from './../../src/actions/types';
import configureMockStore from 'redux-mock-store'
jest.mock('./../../src/config/db.js', () => {
const firebasemock = require('firebase-mock');
const mockdatabase = new firebasemock.MockFirebase();
const mockauth = new firebasemock.MockFirebase();
const mocksdk = new firebasemock.MockFirebaseSdk(path => {
return path ? mockdatabase.child(path) : mockdatabase;
}, () => {
return mockauth;
});
const firebase = mocksdk.initializeApp(); // can take a path arg to database url
// optional - expose the mock
global.firebase = firebase;
// return the mock to match your export api
return firebase;
});
const middlewares = [thunk]
const mockStore = configureMockStore(middlewares)
describe("Action Aluno", () => {
let store;
beforeEach(() => {
store = mockStore({ alunos: [] });
})
test("Adicionar aluno", () => {
spyOn(actions, 'fetchAlunos');
const expectedAction = {
type: types.FETCH_ALUNOS
}
var newAluno = {
nome: 'Teste aluno'
};
return store.dispatch(actions.addAluno(newAluno)).then(() => {
console.log('store ==>', store);
console.log(store.getActions());
expect(store.getActions()).toEqual(expectedAction);
})
});
test('should create an action to add a todo', () => {
const alunos = [];
const expectedAction = {
type: types.FETCH_ALUNOS,
alunos
}
return store.dispatch(actions.fetchAlunos(alunos)).then(() => {
console.log('store ==>', store);
expect(store.getActions()).toEqual(expectedAction);
})
// expect(mockFirebaseService).toEqual(expectedAction)
})
})
但是告诉我这个错误:
动作Aluno ×Adicionar aluno(45ms) ×应该创建一个动作来添加待办事项(2毫秒)
●Action Aluno› Adicionar aluno
TypeError: snap.forEach is not a function
9 | .ref('/alunos')
10 | .on('value', snap => {
> 11 | snap.forEach(snapChild => {
| ^
12 | var item = snapChild.val();
13 | item.key = snapChild.key;
14 |
at src/actions/aluno.js:11:10
at Object.dispatch (node_modules/redux-thunk/lib/index.js:11:18)
at Object.<anonymous> (__tests__/actions/aluno.test.js:50:18)
●动作Aluno›应该创建一个动作来添加待办事项
TypeError: snap.forEach is not a function
9 | .ref('/alunos')
10 | .on('value', snap => {
> 11 | snap.forEach(snapChild => {
| ^
12 | var item = snapChild.val();
13 | item.key = snapChild.key;
有人帮助我,在我的搜索中找不到用于测试Firebase的教程。
答案 0 :(得分:0)