小编phy*_*boy的帖子

我是否可以在不需要[formGroup]的情况下使用[formControl]?

我在用户界面中使用了下拉菜单,但是当我使用它时,却[formControl]抛出了错误:

找不到具有未指定名称属性的控件

我在中使用ReactiveFormsModule app.module.ts

我已经用过Google,发现解决方案是在父级中使用[formGroup] div,但是我不确定如何正确地实现,因为我是formControl从中定义我的subscribe

myComp.component.html

<div class="exceptional-status">
  <select #exceptionalSelect id="excep-status-dropdown" [formControl]="select">
    <option disabled value="default">--Exceptional statuses--</option>
    <!-- other options here with *ngFor -->
  </select>
</div>
Run Code Online (Sandbox Code Playgroud)

myComp.component.ts

select: FormControl;

mySubscription() {
  this.myService.myFunct.subscribe(res => {
    this.select = new FormControl(res.status)
  });
}
Run Code Online (Sandbox Code Playgroud)

html javascript typescript angular

5
推荐指数
1
解决办法
2792
查看次数

根据条件添加或删除类

classList我正在根据变量\xe2\x80\x99s 的真实性从元素\xe2\x80\x99s 添加/删除一个类。然而,我\xe2\x80\x99m 这样做似乎是一种迟钝的方式:

\n
if (myConditionIsMet) {\n  myEl.classList.add("myClass");\n} else {\n  myEl.classList.remove("myClass");\n}\n
Run Code Online (Sandbox Code Playgroud)\n

有没有一种方法可以让这个更性感并动态调用add/remove链式函数,例如使用条件运算符,例如:

\n
myEl.classList.{myConditionIsMet ? add(\'myClass\') : remove(\'myClass\')};\n
Run Code Online (Sandbox Code Playgroud)\n

当然,上面是伪代码,我希望尽可能简单的 JS。

\n

html javascript css

5
推荐指数
2
解决办法
4433
查看次数

监视 bunyan 日志 - NodeJS

有什么方法可以监视 bunyan 日志以确保打印出我期望的内容?

我的文件

const bunyan = require('bunyan');
const log = bunyan.createLogger({name: 'FailureAuditService'});

class someClass {
   someFunct() {
     if(x) {
        log.warn('something happened');
     }
   }
}
Run Code Online (Sandbox Code Playgroud)

测试

const service = require(../MyFile);

describe('test something', () => {
    it('Will test the bunyan log', res => {
       let consoleLog = sinon.spy(log, 'createLogger');
       let x = true;

       service.someClass(x).then(res => {
          let expected = 'something happened';
          consoleLog.should.equal(expected);
       });
    });
})
Run Code Online (Sandbox Code Playgroud)

javascript testing node.js sinon bunyan

4
推荐指数
1
解决办法
1194
查看次数

Angular - 单元测试按键

我有一个检测按键的功能,如果按键按下 = 转义,则会触发一个功能。

我在伪造要传入的 KeyboardEvent 本身时遇到了麻烦。

我看到了这篇文章,但是实现这个解决方案会产生以下输出(我控制台记录了事件本身):

日志:KeyboardEvent{isTrusted:false} Chrome 68.0.3440 (Mac OS X 10.13.6) 当按下 ESCAPE 按钮时,ConfirmationComponent 应该调用 onDeny FAILED 预期的 spy onDeny 已被调用。

组件.ts

@HostListener('window:keyup', ['$event'])
  keyEvent(event: KeyboardEvent) {
    console.log(event);
    // Press escape - close dialog with simulated 'cancel' click
    if (event.code === 'Escape') {
      this.onDeny();
    }
  }

  onDeny() {
     // something is done here
  }
Run Code Online (Sandbox Code Playgroud)

测试文件

it('should autofocus on cancel button on init', () => {
    spyOn(component, 'onDeny');
    component.keyEvent(ESCAPE);
    expect(component.onDeny).toHaveBeenCalled();
  });
Run Code Online (Sandbox Code Playgroud)

javascript testing karma-runner angular

4
推荐指数
1
解决办法
8487
查看次数

从我的组件单元测试测试服务调用

我如何简单地测试我的服务是否被调用?

myComp.ts

constructor(private myService: MyService) {}

myFunction() {
   this.myService.someFunction();
}
Run Code Online (Sandbox Code Playgroud)

myTest.ts

beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [ReactiveFormsModule, FormsModule, HttpClientModule],
      declarations: [MyComponent]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(MyComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should delete customer feed', () => {
    const mySpy = spyOn(MyService, 'someFunction');

    component.myFunction();

    expect(mySpy).toHaveBeenCalledTimes(1);
  });
Run Code Online (Sandbox Code Playgroud)

目前,该行const mySpy = spyOn(MyService, 'someFunction');显示红色波浪线,'someFunction'表示:

“someFunction”类型的参数不可分配给原型类型的参数 | 服务 | 网址

根据评论更新 我的设置现在如何:

let myService: MyService;

beforeEach(() => {
  ...
  providers: [MyService],
  ...
  .compileComponents()
});

beforeEach(() => …
Run Code Online (Sandbox Code Playgroud)

testing unit-testing karma-jasmine angular

4
推荐指数
1
解决办法
9192
查看次数

检查 Jest 中一个对象数组是否包含另一个对象数组的元素

我见过这个问题的不同解决方案的实现,但是,似乎没有一个对我有用。

假设我有一个对象数组(长度为 6),其中包含具有以下结构的唯一数据:

{
  first_name,
  last_name,
  age,
  dob,
  address_1,
  postal_code
}
Run Code Online (Sandbox Code Playgroud)

如果此数组包含另一个对象数组的部分元素(其对象的结构稍短),我将如何比较:

{
  first_name,
  last_name,
  age
}
Run Code Online (Sandbox Code Playgroud)

我知道如果我要比较单个项目我可以使用类似的东西:

expect(response[i]).toMatchObject(expected[i]);
Run Code Online (Sandbox Code Playgroud)

但是我不确定如何比较完整数组......

我见过这样的事情:

expect(state).toEqual(          
  expect.arrayContaining([      
    expect.objectContaining({   
      type: 'END'               
    })
  ])
)
Run Code Online (Sandbox Code Playgroud)

但我无法让它发挥作用。

javascript arrays testing jestjs

4
推荐指数
1
解决办法
1万
查看次数

Angular - 将字符串变量的部分设为粗体

有什么方法可以将字符串的一部分分配给变量粗体?

我尝试了以下方法:

boldTxt = 'bold'

message = 'this text should be ' + this.boldTxt.toUpperCase().bold() ;
Run Code Online (Sandbox Code Playgroud)

但是我在 HTML 中得到的是:

this should be <b>BOLD</b>

闪电战

javascript typescript angular

3
推荐指数
1
解决办法
2357
查看次数

Angular - 测试一个附有集合的@Input

我有一个组件,它将参数作为一种指令来设置<div>其模板内的大小,现在唯一的问题是我不确定如何测试它?

我这样称呼我的组件,其中small可以替换为其他预定义的大小,例如medium/large等:

<spinner small></spinner>
Run Code Online (Sandbox Code Playgroud)

然后我将其作为 an@Input(<size>)并执行 set 以更改大小变量,然后将其传递给 an[ngStyle]以更改显示组件大小的 CSS。

组件.ts

size: number;

@Input('small') set small(value) {
  this.size = !value ? 25 : this.size;
}

get getSize() {
  const myStyle = {
    width: this.size + 'px',
    height: this.size + 'px'
  };
  return myStyle;
}
Run Code Online (Sandbox Code Playgroud)

组件.html

<div [ngStyle]="getSize"></div>
Run Code Online (Sandbox Code Playgroud)

我已经成功测试了该get getSize()功能,但我对这些功能有些困惑@Input(<val>) set:-/

javascript testing typescript karma-runner angular

3
推荐指数
1
解决办法
1870
查看次数

使用 Angular 对 CSS 进行单元测试

有什么方法可以测试height元素的 css 属性吗?

我有一个可以改变 a 高度的函数div,最好在测试中检查该函数是否正确运行......

IE:

<div #myDiv>Hello world</div>
Run Code Online (Sandbox Code Playgroud)
@ViewChild('myDiv') myDiv: ElementRef;

myFunct() {
  this.myDiv.nativeElement.style.height = 500;
}
Run Code Online (Sandbox Code Playgroud)
it('should increase div size', () => {
  // Here is where I get stuck...
});
Run Code Online (Sandbox Code Playgroud)

更新

实施测试,例如:

it('should return correct height of dropdown when initialised', () => {
    component.myFunct();

    expect(component.dropdown.nativeElement.style.height).toBe(34);
  });
Run Code Online (Sandbox Code Playgroud)

导致测试失败并显示消息:

预计 '' 为 500。

html css testing angular

3
推荐指数
1
解决办法
6550
查看次数

在 TypeScript 中使用 supertest - 遇到问题“无法调用类型缺少调用签名的表达式”

supertest在最初使用 JavaScript 后,我尝试与 TypeScript 一起使用。

但是,我现在在我的代码下收到红色波浪线,说明:

无法调用类型缺少调用签名的表达式。类型“typeof supertest”没有兼容的调用签名

我通过以下方式使用 supertest:

import * as supertest from 'supertest';

const request = superTest(some.url);

// Forgive my pseudocode
function makeRequest() {
  return request.get('/endpoint')...
}
Run Code Online (Sandbox Code Playgroud)

这是我应该担心的事情吗?我该如何纠正?

javascript typescript supertest

3
推荐指数
2
解决办法
1937
查看次数