在Redux的Writing Tests部分中,store.dispatch(actions.fetchTodos())
如果fetch
字面上调用store.dispatch
,actions.fetchTodos
如何不调用it('creates FETCH_TODOS_SUCCESS when fetching todos has been done', (done) => {
nock('http://example.com/')
.get('/todos')
.reply(200, { todos: ['do something'] })
const expectedActions = [
{ type: types.FETCH_TODOS_REQUEST },
{ type: types.FETCH_TODOS_SUCCESS, body: { todos: ['do something'] } }
]
const store = mockStore({ todos: [] }, expectedActions, done)
store.dispatch(actions.fetchTodos())
})
方法?
static X509Certificate2 GetCertificateFromStore(string thumbprint)
{
var store = new X509Store(StoreLocation.CurrentUser);
try
{
thumbprint = Regex.Replace(thumbprint, @"[^\da-zA-z]", string.Empty).ToUpper();
store.Open(OpenFlags.ReadOnly);
var certCollection = store.Certificates;
var currentCerts = certCollection.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
var signingCert = currentCerts.Find(X509FindType.FindByThumbprint, thumbprint, false);
if (signingCert.Count == 0)
{
throw new Exception($"Could not find Xero SSL certificate. cert_name={thumbprint}");
}
return signingCert[0];
}
finally
{
store.Close();
}
}
答案 0 :(得分:2)
它正在调用fetch
方法,但调用nock
行:
nock('http://example.com/')
.get('/todos')
.reply(200, { todos: ['do something'] })
存根HTTP请求,以便fetch
只返回正确的数据(实际上没有命中端点)。
另一种选择是将对fetch
的调用提取到另一个模块 - 比如api
- 并在您的操作创建者中使用它。在测试中,您只需在api
模块上模拟适当的方法。