EasyMock例子

使用easyMock高级语法,测试响应式数据

{
  "data": {
    default: 'hhh',
    //_req:用来接收用户传入的参数
    _req: function({
      _req
    }) {
      return _req
    },

    name: function({
      _req,
      Mock
    }) {
      if (_req.query.name === 'nk') {
        return _req.query.name + '_' + Mock.mock('@name')
      } else {
        return this.default
      }
    },


    result: function({
      _req,
      Mock
    }) {
      const arr = [{
        id: '1',
        name: '小明',
        age: 18,
        gende: 'male'
      }, {
        id: '2',
        name: '小芳',
        age: 16,
        gende: 'female'
      }];
      var newArr = [];
      //根据年龄查询数据
      for (let i = 0; i < arr.length; i++) {
        //age:规定用户调用该接口的时候需要传入的参数
        if (arr[i].age == _req.query.age) {
          newArr.push(arr[i])
        }
      }
      return newArr
    }
  }
}

此模拟接口的目的为获取竞品报告,返回店铺诊断结果,竞品诊断结果,报告描述,根据传进的goodId返回相应的goodId,其他信息不变;
请求参数
参数名 必选 类型 说明
shopId 是 Long 当前店铺id
reportId 否 Long 工单报告id
goodsCommentCount 否 String 商品评价数,如[{“goodsId”:1,”competitive”:false}]

{
  "code": 0,
  "msg": "我是返回码信息",
  "data": function({
    _req,
    Mock
  }) {

    let new_data = {
      "shopGoodsDiagnosisResult": null,
      "competitiveGoodsDiagnosisResult": [],
      "reportDescribe": [{
        "type": 1,
        "content": "我是优劣势分析"
      }, {
        "type": 2,
        "content": "我是优化建议"
      }],

    }

    let init_obj;

    try {
      /* code */
      let goodsCommentCount = JSON.parse(_req.query.goodsCommentCount);


      for (let i = 0; i < goodsCommentCount.length; i++) {
        if (goodsCommentCount[i].competitive) {
          init_obj = {
            "goodsName": "我是商品标题",
            "picUrl": "我是商品图片",
            "sales": 100,
            "commentCount": 100,
            "totalScore": 6,
            "naturalPvScore": 5,
            "naturalPvIsExcellent": false,
            "excellentWordsIsExcellent": false,
            "excellentWords": [
              "关键词1",
              "关键词2",
              "关键词3"
            ],
            "titleLength": 55,
            "illicitWords": [
              "关键词1",
              "关键词2",
              "关键词3"
            ],
            "specialSymbols": [
              "-",
              "_"
            ]
          }
          init_obj.goodsId = Number(goodsCommentCount[i].goodsId);
          new_data.competitiveGoodsDiagnosisResult.push(init_obj);
        } else {
          init_obj = {
            "goodsName": "我是商品标题",
            "picUrl": "我是商品图片",
            "sales": 100,
            "commentCount": 100,
            "totalScore": 6,
            "naturalPvScore": 5,
            "naturalPvIsExcellent": false,
            "excellentWordsIsExcellent": false,
            "excellentWords": [
              "关键词1",
              "关键词2",
              "关键词3"
            ],
            "titleLength": 55,
            "illicitWords": [
              "关键词1",
              "关键词2",
              "关键词3"
            ],
            "specialSymbols": [
              "-",
              "_"
            ]
          }
          init_obj.goodsId = Number(goodsCommentCount[i].goodsId);
          new_data.shopGoodsDiagnosisResult = init_obj;
        }
      }

    } catch (e) {
      return e.message;
    }

    if (new_data.competitiveGoodsDiagnosisResult.length < 0) {
      new_data.competitiveGoodsDiagnosisResult = null;
    }


    return new_data;

  },
}

你可能感兴趣的:(EasyMock例子)