我正在创建一个简单的操作来使用Thunk从API获取一些数据。它看起来像这样:
SELECT TOP 1 HasConditionFlag FROM vCustomerDetails
WHERE vCustomerDetails.UserID = @user_id
ORDER BY EntryDate DESC
应该拨打module Customer
class CustomerUsage < ActiveRecord::Base
self.table_name = 'vCustomerDetails'
def self.has_condition_flag(user_id)
vCustomerDetails
.where("vCustomerDetails.UserID = #{user_id}")
.order('vCustomerDetails.EntryDate DESC')
.last
end
end
end
一次。我已经验证它是这样做的,因为它在Web浏览器中调用时成功提取数据。但是,当我写这个测试时:
import fetch from 'isomorphic-fetch';
function json(response) {
return response.json();
}
/**
* Fetches books from the server
*/
export function getBooks() {
return function(dispatch) {
fetch("http://localhost:1357/book", {mode: "cors"})
.then(json)
.then(function(data) {
dispatch({
type: "GET_BOOKS",
devices: data
});
});
}
};
但是,此测试失败,并且fetch
的调用计数为0.我怀疑这是由于在测试之前由操作导入了fetch,这就是为什么间谍是在文件的顶部。但是,这不起作用。测试import fetch from 'isomorphic-fetch';
let spy = sinon.spy(fetch);
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import {getBooks} from '../../actions/getBooks';
import sinon from 'sinon';
const middlewares = [ thunk ];
const mockStore = configureMockStore(middlewares);
describe('async actions', () => {
it('calls the server', () => {
const store = mockStore({books: []});
store.dispatch(getBooks());
expect(spy.callCount).toEqual(1);
spy.restore();
});
});
被调用的推荐方法是什么?
答案 0 :(得分:1)
从http://arnaudbenard.com/redux-mock-store/读取部分异步操作。
我猜测它发生是因为你没有在测试中使用承诺。
it('calls the server', (done) => {
const store = mockStore({books: []});
store.dispatch(getBooks()).then(() => {
expect(spy.callCount).toEqual(1);
spy.restore();
done();
});
});