小程序后台微信用户注册

 

Mockito for Java

http://site.mockito.org/

代码在controllers/user.js

bet36体育在线,参照文书档案:
http://www.jianshu.com/p/c1494681400b

***  参考文书档案 
  365best足彩,https://github.com/strongself/fabricio/blob/develop/docs/api\_reference.md  ***

1.无法mock

365bet现场滚球,就自笔者熟习的, 也是采纳最广的两门语言 C++ 和 Java 来看

gmock 和 mockito 在大部分状态下都够用了,一般情状下不须求也不该 mock
私有主意,静态方法和大局方法,当然假诺你的代码可测试性及正视反转做得得没那么好,
实在绕不过去,也有回旋之法, C++能够直接改掉其在内部存储器中的函数地址, Java
能够使用反射或改动字节码来消除.

那是贰个 HTTPS 接口,开发者服务器使用报到凭证 code获取 session_key
和 openid。

//Moya 10的版本已经去掉了RxMoyaProvider代码,直接用MoyaProvider
let provider = MoyaProvider<MyService>()
provider.rx.request(.setUserStatus)
            .asObservable().mapJSON()
            .mapObject(type: UserStatusModel.self)
            .subscribe { [weak self] event in
                if self == nil{
                    return
                }
                switch event {
                case let .next(response):
                    //............
                    break
                case let .error(error):
                    print(error)
                    //这个地方,处了网络异常外,对错误码也可处理
                    if let err = error as? NSError{
                        if err.domain == "Network"{
                            switch err.code{
                            case 401:
                            print("param invalide")
                            break
                            default:
                            print("other error")    
                            }
                        }else{
                            print("other error") 
                        }
                    }else{
                        print("other error") 
                    }
                    break
                default:
                    break
                }
            }.disposed(by: disposeBag)

 

参考资料

  • https://github.com/google/googletest/blob/master/googlemock/docs/ForDummies.md
  • https://github.com/google/googletest/blob/master/googlemock/docs/CheatSheet.md
  • https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md
  • Mockito
    官方文档
  • Mockito
    Refcard
  • 怎样写出好测试

session_key
是对用户数量开展加密签署的密钥。为了我行使安全,session_key
不应该在互连网上传输

func setUserStatus() {
    let parameters:[String : Any] = ["userid": userid!]
    Alamofire.request(URL_SET_USER_STATUS, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: nil).responseObject{[weak self]
        (response :DataResponse<StatusModel>) in
        if self == nil{
            return
        }
        let ret = response.result.value
        if response.result.error != nil{
            ......
        }else{
            .......
        }
    }
}

 返回值

Mock 的问题

mock的时候最烦人的是四个难点

之所以函数体里可看出 doc.errcode, doc.errmsg 正是其一错误代码和错误音信了

这样我们能够对那几个错误码举行统一处理,data数据解析成功后回来

  

Mock 类库和工具

仅就本身所熟习的 Java 和 C++ 举例如下, python, ruby, JavaScript
之类的脚本语言就更简短了

拜读了下skyvow大神的m-mall-admin后台,

{
"code": 200,
"message": "",
"data": {}
}

四 、请求接口   接口文档出处 
  https://github.com/strongself/fabricio/blob/develop/docs/api\_reference.md 

Mock 依赖的类和措施

大旨步骤:

  1. mock 设置模拟行为
  2. call 调用被测试代码
  3. verify 检验期望行为

这里以 Guava Loading Cache
类为例, 测试它的基本行为是还是不是切合预期

package com.github.walterfan.hellotest;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.RemovalCause;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
import com.google.common.util.concurrent.Uninterruptibles;
import lombok.extern.slf4j.Slf4j;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

/**
 * Created by yafan on 23/1/2018.
 */
@Slf4j
public class LoadingCacheTest {
    private LoadingCache<String,  String> internalCache;

    @Mock
    private CacheLoader<String, String> cacheLoader;

    @Mock
    private RemovalListener<String, String> cacheListener;

    @Captor
    private ArgumentCaptor<RemovalNotification<String, String>> argumentCaptor;

    private Answer<String> loaderAnswer;

    private AtomicInteger loadCounter = new AtomicInteger(0);

    @BeforeMethod
    public void setup() {

        MockitoAnnotations.initMocks(this);

        this.internalCache = CacheBuilder.newBuilder()
                .maximumSize(3)
                .expireAfterWrite(1, TimeUnit.SECONDS)
                .removalListener(this.cacheListener)
                .build(this.cacheLoader);

        this.loaderAnswer = new Answer<String>() {
            @Override
            public String answer(InvocationOnMock invocationOnMock) throws Throwable {
                String key = invocationOnMock.getArgumentAt(0, String.class);
                switch(loadCounter.getAndIncrement()) {
                    case 0:
                        return "alice";
                    case 1:
                        return "bob";
                    case 2:
                        return "carl";
                    default:
                        return "unknown";
                }
            }
        };
    }

    @Test
    public void cacheTest() throws Exception {
        //Mock the return value of loader
        //Mockito.when(cacheLoader.load(Mockito.anyString())).thenReturn("alice");
        Mockito.when(cacheLoader.load(Mockito.anyString())).thenAnswer(loaderAnswer);

        assertTrue("alice".equals(internalCache.get("name")));

        //sleep for 2 seconds
        Uninterruptibles.sleepUninterruptibly(2, TimeUnit.SECONDS);
        assertTrue("bob".equals(internalCache.get("name")));

        verify(cacheLoader, times(2)).load("name");
        verify(cacheListener).onRemoval(argumentCaptor.capture());

        assertEquals(argumentCaptor.getValue().getKey(), "name");
        assertEquals(argumentCaptor.getValue().getValue(), "alice");
        assertEquals(argumentCaptor.getValue().getCause(), RemovalCause.EXPIRED);
    }
}

回来参数:

3,moya的行使示例
主导的使用示例:https://github.com/Moya/Moya/blob/master/docs/Examples/Basic.md
如出一辙的接口封装成moya后

{
    "access_token": "ccccccccccccccccccccccc",
    "token_type": "bearer",
    "expires_in": 86400,
    "refresh_token": "refresh_tokenqqqqqqqqqqqqq", 
   "scope": "organizations apps issues features account twitter_client_apps beta software answers" 
}

Mock 静态方法

那边运用 Powermock 和 testng , 假设有 junit 的话, 用法稍有分化
testng 需要从 PowerMockTestCase 继承
junit4 必要添加二个评释 @RunWith(PowerMockRunner.class)

  • 静态类和格局

package com.github.walterfan.hellotest;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;


@Slf4j
public class FileUtils {

    public static final FileFilter javaFileFilter = new FileFilter() {
        @Override
        public boolean accept(File file) {

            if(file.isDirectory()) {
                return true;
            }
            if(file.getName().endsWith(".java")) {
                return true;
            }

            return false;
        }
    };

    public static List<String> listFiles(File folder, FileFilter filter) {
        List<String> files = new ArrayList<>();
        listDir(new File("."), files, filter);
        return files;
    }

    public static void listDir(File folder, List<String> fileNames, FileFilter filter) {
        File[] files = folder.listFiles(filter);
        for (File file: files) {
            if(file.isFile()) {
                fileNames.add(file.getName());
            } else if (file.isDirectory()) {
                listDir(file, fileNames, filter);
            }
        }
    }
}
  • 测试类

package com.github.walterfan.hellotest;



import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.testng.PowerMockObjectFactory;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.IObjectFactory;
import org.testng.annotations.Test;


import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.List;

import static org.mockito.Matchers.eq;
import static org.testng.Assert.assertEquals;

//@RunWith(PowerMockRunner.class) -- for junit4
@PrepareForTest(FileUtils.class)
public class FileUtilsTest extends PowerMockTestCase {

    public  int howManyFiles(String path, FileFilter filter) {
        System.out.println("-----------");
        List<String> files = FileUtils.listFiles(new File(path), filter);
        files.forEach(System.out::println);
        return files.size();
    }


    @Test
    public void testHowManyFiles() {

        List<String> fileNames = Arrays.asList("a.java", "b.java", "c.java");
        PowerMockito.mockStatic(FileUtils.class);
        PowerMockito.when(FileUtils.listFiles(Mockito.any(), Mockito.any())).thenReturn(fileNames);

        int count = howManyFiles(".", FileUtils.javaFileFilter);
        assertEquals(count, 3);
    }
}

在 pom.xml 中加上

<dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-core</artifactId>
            <version>1.7.1</version>
            <scope>test</scope>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito</artifactId>
            <version>1.7.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>1.7.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-testng</artifactId>
            <version>1.7.1</version>
            <scope>test</scope>
        </dependency>

openid    用户唯一标识

4,ObjectMapper
赢拿到网络数据后,必要将json解析成对象数据,可协作ObjectMapper一起用。一般服务端再次来到的多寡是

bet36体育在线 1bet36体育在线 2

Mock Server

MockServer 用来 mock 整个web service
https://github.com/jamesdbloom/mockserver

session_key    会话密钥

迄今,rxswift + moya就足以健康使用了
理所当然,moya的比较alamofire还有许多好用的效率,前边再跟大家享用

  1 <?php
  2 
  3 class ScriptUserDaily 
  4 {
  5   //保存第一次获取的 refresh_token ,用于下次刷新token用
  6     private $filePath = '/files/fabricToken.json';
  7 
  8     public function fire()
  9     {
 10         $access_token = $this->getRefreshToken();
 11         if(empty($access_token))
 12             $access_token = $this->getToken();
 13 
 14         $edata =  time();
 15         $sdata = $edata - 24 * 3600 * 5;
 16 
 17         $header = [
 18             "Authorization: Bearer ".$access_token
 19         ];
 20 
 21      //数据库获取应用,主要获取  organization_id  和 app_id
 22         $appinfo = FanAppInfo::byFabric()->get();
 23         foreach ($appinfo as $appItem){
 24             $fabricid = $appItem->fabricid;
 25             if(empty($fabricid))
 26                 continue;
 27 
 28        $organization_id = $appItem->organization_id;
 29             $url = "https://fabric.io/api/v2/organizations/$organization_id/apps/$fabricid/growth_analytics/daily_active.json?start=$sdata&end=$edata";
 30 
 31             $this->getDatas($url,$header,1);
 32 
 33             $url2 = "https://fabric.io/api/v2/organizations/$organization_id/apps/$fabricid/growth_analytics/daily_new.json?start=$sdata&end=$edata";
 34 
 35             $this->getDatas($url2,$header,2);
 36         }
 37     }
 38 
 39     private function getRefreshToken(){
 40         //获取 refresh_token 从文件中读取保存的refresh_token
 41         $path = $this->filePath;
 42 
 43         $JsonData = file_get_contents($path);
 44 
 45         $rejson = json_decode($JsonData, true);
 46         $refresh_token = $rejson['refresh_token'];
 47 
 48         //刷新 token
 49         $url = 'https://fabric.io/oauth/token';
 50         $header = [
 51             "content-type: application/json"
 52         ];
 53         $body = [
 54             'grant_type' => 'refresh_token',
 55             'refresh_token' => trim($refresh_token)
 56         ];
 57 
 58         $data = $this->curl_post($url,$header,json_encode($body));
 59         $rejson = json_decode($data, true);
 60 
 61         $access_token_new = '';
 62         $refresh_token_new = '';
 63         if(isset($rejson['refresh_token']))
 64             $refresh_token_new = $rejson['refresh_token'];
 65         if(isset($rejson['access_token']))
 66             $access_token_new = $rejson['access_token'];
 67 
 68         if(!empty($refresh_token_new)){
 69             $txt = [
 70                 'access_token' => $access_token_new,
 71                 'refresh_token' => $refresh_token_new
 72             ];
 73 
 74             //重新写入新的 refresh_token
 75            $this->writeRefreshToken($txt);
 76         }
 77 
 78         return $access_token_new;
 79     }
 80 
 81     private function getToken(){
 82         $url = 'https://fabric.io/oauth/token';
 83         $header = [
 84             "content-type: application/json"
 85         ];
 86         $body = [
 87             'grant_type' => 'password',
 88             'scope' => 'organizations apps issues features account twitter_client_apps beta software answers',
 89             'username' => '14141414@qq.com',
 90             'password' => '123456789',
 91             'client_id' => '2c18f8a77609ee6bbac9e53f3768fedc45fb96be0dbcb41defa706dc57d9c931',
 92             'client_secret' => '092ed1cdde336647b13d44178932cba10911577faf0eda894896188a7d900cc9'
 93         ];
 94 
 95         $data = $this->curl_post($url,$header,json_encode($body));
 96         $rejson = json_decode($data, true);
 97 
 98         $access_token_new = '';
 99         $refresh_token_new = '';
100         if(isset($rejson['refresh_token']))
101             $refresh_token_new = $rejson['refresh_token'];
102         if(isset($rejson['access_token']))
103             $access_token_new = $rejson['access_token'];
104 
105         if(!empty($refresh_token_new)){
106             $txt = [
107                 'access_token' => $access_token_new,
108                 'refresh_token' => $refresh_token_new
109             ];
110 
111             //重新写入新的 refresh_token
112             $this->writeRefreshToken($txt);
113         }
114 
115         return $access_token_new;
116     }
117 
118     private function writeRefreshToken($txt){
119         $path = $this->filePath;
120 
121         $myfile = fopen($path, "w");
122         $txt = json_encode($txt);
123         fwrite($myfile,$txt);
124         fclose($myfile);
125     }
126 
127     private function getDatas($url,$header,,$type){
128         $resData = $this->curl_get($url,$header);
129         $datas = json_decode($resData, true);
130 
131         if(!isset($datas['series']))
132             return '';
133 
134         $active = 0;
135         $news = 0;
136         foreach ($datas['series'] as $item){
137             $date = date('Y-m-d',$item[0]);
138 
139             if($type == 1){
140                 $active = intval($item[1]);
141             }elseif($type == 2){
142                 $news = intval($item[1]);
143             }
144 
145            //处理数据
146 
147         }
148     }
149 
150     private function curl_get($url, $header = [], $time = 5){
151       $ch = curl_init();
152       curl_setopt($ch, CURLOPT_URL, $url);
153       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
154       curl_setopt($ch, CURLOPT_HEADER, 0);
155       if (!empty($header)) {
156           curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
157       }
158       curl_setopt($ch, CURLOPT_TIMEOUT, $time);
159       $result = curl_exec($ch);
160       curl_close($ch);
161       return $result;
162   }
163 
164   private function curl_post($url, $header = [], $body = [], $time = 5){
165       $ch = curl_init();
166       curl_setopt($ch, CURLOPT_URL, $url);
167       curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
168       if (!empty($body)) {
169           curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
170       }
171       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
172       curl_setopt($ch, CURLOPT_HEADER, 0);
173       if (!empty($header)) {
174           curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
175       }
176       curl_setopt($ch, CURLOPT_TIMEOUT, $time);
177       $result = curl_exec($ch);
178       curl_close($ch);
179       return $result;
180   }
181 }

2. mock整个类或接口

与那些类或接口的相互整体mock 掉,接口也可指某些API

对于服务器端微信用户注册和登录一贯未曾搞驾驭逻辑,看了两次代码,总算有点概念了

public protocol TargetType {

    /// The target's base `URL`.
    var baseURL: URL { get }

    /// The path to be appended to `baseURL` to form the full `URL`.
    var path: String { get }

    /// The HTTP method used in the request.
    var method: Moya.Method { get }

    /// Provides stub data for use in testing.这个数据是用在api测试时用的
    var sampleData: Data { get }

    /// The type of HTTP task to be performed.
    var task: Task { get }

    /// Whether or not to perform Alamofire validation. Defaults to `false`.
    var validate: Bool { get }

    /// The headers to be used in the request.
    var headers: [String: String]? { get }
}

     *** 
将紫褐的记录下来  organization_id  和  app_id  ***

Mock 的原则

Mockito 是大面积选拔的 Java Mock library, 它的 wiki 上有篇著作 –
怎样写出好的测试代码,
当中提议了几条选取 mock 的标准:

  • 决不 mock 非你抱有的项目
  • 不要 mock 值对象
  • 永不 mock 全体的东西

后两点很好通晓, 第③点多少语焉不详, 什么叫非你富有的花色,
我的驾驭便是只要贰个类型不是您与第叁方约定的接口, 它属于旁人定义的,
你只是拿过来使用, 那么您最好不要去mock 它, 你能够写2当中间层或适配器,
然后mock 那当中间层和适配器, 原因是第2方能够随时变动它的概念和作为,
你把它mock掉了, 你也就不会发觉由于外人改动了概念或作为导致的卓殊.
而你本身写的中间层由你掌控,不必有此担心。

与第2方或其余服务集成测试属于Consumer Test 消费者测试和End to End
端到端的测试的限定

.then(doc 若是doc不为null,表明用户名早已存在,就无须再一次登记了嘛,

各种接口都亟需拼接这么些消息,包涵api路径,请求的章程,参数,参数编码格式,音信头等多少个重一大半,获取到网络数据后,再分析数据进行处理。
2,moya
moya的中文表明:https://github.com/Moya/Moya/blob/master/Readme\_CN.md
它对网络请求的url和parameter进行了更深的包装
TargetType那么些是应用moya必需要落实的二个合计,那么些里面我们着力得以看到它包裹的关键内容

发表评论

电子邮件地址不会被公开。 必填项已用*标注