小编Dae*_*esi的帖子

angular2单元测试:无法读取componentInstance.method()的属性未定义

mycomponent.spec.ts类:

这将引发错误:无法读取未定义的属性'ngOnInit'。

let myComponent: MyComponent;
let myService: MyService;

describe('myComponent', () => {
   beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [MyComponent],
    providers: [
      {provide: MyService, useClass: MockMyService} // **--passing Mock service**
  ]
}).compileComponents()
  .then(() => {
    myComponent = TestBed.createComponent(MyComponent).componentInstance;
    myService = TestBed.get(MyService);
    console.log(myService.getData());
  });
});

it('should get the mock data', () => {
   myComponent.ngOnInit(); //-----------> seems like myComponent is not defined at this place, how to resolve this error 
   expect(myComponent.data).toBe(DATA_OBJECT);
  });
});
Run Code Online (Sandbox Code Playgroud)

以下是MyComponent:

@Component({
  selector: 'pm-catalogs',
  templateUrl: './catalog-list.component.html'
})

export class MyComponent implements …
Run Code Online (Sandbox Code Playgroud)

unit-testing jasmine typescript karma-jasmine angular

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

AttributeError:使用pytest的monkeypatch时

src/mainDir/mainFile.py

mainFile.py 的内容

import src.tempDir.tempFile as temp

data = 'someData'
def foo(self):
    ans = temp.boo(data)
    return ans
Run Code Online (Sandbox Code Playgroud)

src/tempDir/tempFile.py

def boo(data):

   ans = data
   return ans
Run Code Online (Sandbox Code Playgroud)

现在我想测试foo()并在方法中src/tests/test_mainFile.py模拟temp.boo(data)方法foo()

 import src.mainDir.mainFile as mainFunc

  testData = 'testData'
  def test_foo(monkeypatch):
     monkeypatch.setattr('src.tempDir.tempFile', 'boo', testData)
     ans = mainFunc.foo()
     assert ans == testData
Run Code Online (Sandbox Code Playgroud)

但我收到错误

属性错误:“src.tempDir.tempFile”没有属性“boo”

我期望 ans = testData.

我想知道我是否正确地模拟了我的 tempDir.boo() 方法,或者我应该使用 pytest 的模拟程序而不是 Monkeypatch。

python monkeypatching pytest

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