代码之家  ›  专栏  ›  技术社区  ›  MMelvin0581

莫须有嘲讽

  •  0
  • MMelvin0581  · 技术社区  · 6 年前

    我有一个默认的AXIOS实例,我安装在测试中:

    // http.js
    import Axios from 'axios'
    
    export const instance = Axios.create({
      baseURL: 'https://cs-node-123.herokuapp.com'
    })
    

    在我正在测试的服务和测试中导入:

    // RatingsService.js
    import { instance } from '@/http'
    import RatingsFactory from '@/models/Ratings/RatingsFactory'
    import config from '@/utils/config'
    
    const endpoint = 'api/rac/v1/ratings'
    
    export default class RatingsService {
    
      static async hasRated(objGuid) {
        try {
          const result = await instance.get(endpoint, {
            params: {
              obj_guid: objGuid,
              user_guid: config.store.state.auth.user.userId
            }
          })
          return RatingsFactory(result.data)
        } catch (error) {
          throw error
        }
      }
    
      ...
    }
    // RatingsService.spec.js
    import RatingsService from '../RatingsService'
    import { instance } from '@/http'
    import moxios from 'moxios'
    
    describe('RatingsService.js', () => {
      beforeEach(() => {
        moxios.install(instance)
      })
      afterEach(() => {
        moxios.uninstall(instance)
      })
    
      it('handles the call to hasRated', async () => {
        moxios.wait(() => {
          const request = moxios.requests.mostRecent()
          console.log(request)
          request.respondWith({
            status: 200,
            response: {}
          })
        })
        await RatingsService.hasRated('123')
        expect(1).toBe(2)
      })
    })
    

    代码调用并执行请求,而MyXOS由于某种原因不接收它。我也试过进来 axios moxios.install(axios) 但这也不管用。我也准备好了 instance.defaults.adapter axios.defaults.adapter moxios.install(instance) 但那也不管用…

    感谢您的帮助。

    0 回复  |  直到 6 年前