3

近期团队协作总结

 3 years ago
source link: https://segmentfault.com/a/1190000040551118
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

近期团队协作总结

发布于 8 月 20 日

首先我最主要的问题就是心急,对于项目任务分配不明确,不能够很好的将当前任务分工,导致每次提交pr代码量相对比较大,不仅增加自己的出错概率和测试难度,同时也给其他的协作成员带来很大的难度。

1.写代码前记得先拉去最新代码
2.将当前分支代码reset之前记得保存已修改的代码后切换到其他分支进行操作
3.合理领取任务,必要时再将任务划分,保证当前任务量不应过大
4.写代码之前,首先确保当前自己已经领取该issue,防止写完后与其他人重复
5.汉语变英文不要直接变拼音,翻译
6.多加注释

编写完后台登录提交之后,机器人一直报错,当打开报错提示:apiUrl不存在于envirment中!
image.png

在我的本地执行了机器人的命令ng build后也报错:
image.png

找到错误文件,根据该报错提示点进去:
image.png

这不是有apiUrl嘛!怀着疑问去问了潘老师:去看angular.json

image.png

根据这个大概意思猜想是某种情况下用environment.prod文件代替environment文件

通过查询得知:

// This file can be replaced during build by using the fileReplacements array.
// ng build ---prod replaces environment.ts with environment.prod.ts.
// The list of file replacements can be found in angular.json.(environment.ts文件的注释)
通过上面的注释,我们知道在执行 ng build ---prod 命令,进行项目构建的时候,会执行文件替换操作。而相应的文件替换规则,在 angular.json 文件中定义:

"architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
           //...
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
            }
          }
        }
}

不同的开发环境需要不同的接口,例如当我们进行开发测试的时候可能接口为:http://localhost:8080,但是当项目上线后一般是另一个地址,所以需要配置不同的环境变量。使用angular cli在初始化项目的时候也会自动生成environment.ts和environment-prod.ts两个文件。

万物开头难,对于我来说,起初我是很反感单元测试的,我不得不承认它对于团队协作的巨大好处,但是由于锻炼比较少,每次测试时候总是不愿意用这种方式进行测试。在这里也感谢潘老师的督促,能够让自己静下心来去了解单元测试每个代码的意思。下面我将用一个具体的单元测试进行举例:

@Test
  void login() throws UnsupportedEncodingException {
    // 复制老项目的,用于添加注释

    // 构造请求url
    String url = "http://localhost:" + port + "/user/login";

    // 由于spring boot不提供自动配置的RestTemplate bean,他会提供一个restTemplateBuilder bean
    // 可用于在需要的时候创建RestTemplate实例。
    // restTemplate用于发起请求,其中exchange可以指定请求方法
    RestTemplate restTemplate = this.restTemplateBuilder.build();
    // 构造一个请求的headers
    HttpHeaders headers = this.getChromeHeaders();

    // 没有认证信息时401
    // 预言发起请求抛出的异常为HttpClientErrorException。
    Assertions.assertThrows(HttpClientErrorException.class, () -> restTemplate.getForObject(url, JSONObject.class));
    try {
      // HttpEntity包括请求头和请求主体
      HttpEntity entity = new HttpEntity(headers);
      // 调用exchange方法发起method为get,url为xxx,请求头和请求主体为entity,返回类型为User实体的
      restTemplate.exchange(url, HttpMethod.GET, entity, User.class);
    } catch (HttpClientErrorException e) {
      // 捕获异常为401,未认证
      Assertions.assertEquals(e.getStatusCode().value(), HttpStatus.UNAUTHORIZED.value());
    }

    // basic认证模式,构造前台传送信息,
    headers = this.getChromeHeaders();
    // 添加认证信息
    String auth = Base64.getEncoder().encodeToString(
            (appProperties.getUsername() + ":" + appProperties.getPassword()).getBytes("utf-8"));
    headers.add("Authorization", "Basic " + auth);

    // 构造一个请求信息,请求头为headers内容
    HttpEntity entity = new HttpEntity(headers);

    // 获取响应实体,通过get请求
    ResponseEntity<User> result = restTemplate.exchange(url, HttpMethod.GET, entity, User.class);
    // 从响应头中获取参数x-auth-token,并断言非空(后台响应之后返回了token)
    String xAuthToken = result.getHeaders().get("x-auth-token").get(0);
    Assertions.assertNotNull(xAuthToken);
    // 获取响应主体,断言它的用户名等于xxx
    User body = result.getBody();
    Assertions.assertEquals(appProperties.getUsername(), body.getUsername());

    // x-auth-token认证
    // 第一次请求分配了一个token,第二次请求直接使用分配的token
    headers = this.getChromeHeaders();
    headers.add("x-auth-token", xAuthToken);
    // 获取完判断获得的user是之前的user。
    User user = restTemplate.exchange(url, HttpMethod.GET, entity, User.class).getBody();
    Assertions.assertEquals(UserInit.username, user.getUsername());
  }

使用敏捷开发(将大的任务不断分配成各个小任务,保证每个小任务最晚当天可以完成)+ 单元测试的方法不仅能够使得任务更明确,同时在单元测试的时候也能对于数据流有一个更深的掌握。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK