https://github.com/sroettering/ng2-mock
An easy way to mock your Angular stuff
https://github.com/sroettering/ng2-mock
angular mocking
Last synced: 2 months ago
JSON representation
An easy way to mock your Angular stuff
- Host: GitHub
- URL: https://github.com/sroettering/ng2-mock
- Owner: sroettering
- License: mit
- Created: 2018-01-05T14:06:27.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-10-17T07:47:27.000Z (over 7 years ago)
- Last Synced: 2025-06-22T19:05:43.543Z (about 1 year ago)
- Topics: angular, mocking
- Language: TypeScript
- Size: 51.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](http://badge.fury.io/js/ng2-mock)
# ng2-mock
An easy way to mock your Angular elements.
_Note_: If you are using Angular < 5 use v0.0.7 of ng2-mock
_Note_: If you are using Angular < 6 use v0.1.0 of ng2-mock
## Currently Supported Angular Elements
- [x] Component
- [x] Pipe
- [x] Directive
- [x] Service
- [ ] Guard
- [ ] Module
## Installation
```
npm install --save-dev ng2-mock
```
## Usage
Just call the `Mock()` function with your desired class(es) and let ng2-mock do the magic.
Mock() accepts an arbitrary number of supported angular elements as an argument and returns their mocked versions.
If it detects an Injectable class, e.g. Services, as input it mocks the class and returns a provider object
```typescript
{ provide: MyInjectableClass, useValue: MockedMyInjectableClass }
```
wrapping the mocked element, so you don't have to override the provider yourself.
__Note:__ In order for a mocked service method to return a value other than `undefined` you have to use spies (`Jasmine.spyOn()` for
example).
## Example
```typescript
import { Mock } from 'ng2-mock';
describe('AppComponent', () => {
let app: AppComponent;
let fixture: ComponentFixture;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent,
Mock(MyAComponent),
Mock(MyBComponent, MyCComponent, APipe),
],
providers: [
Mock(MyAService),
Mock(MyBService, MyCService, MyDService),
],
}).compileComponents();
beforeEach(() => {
fixture = TestBed.createComponent(MapComponent);
app = fixture.componentInstance;
fixture.detectChanges();
});
}));
it('should create the app', () => {
expect(app).toBeTruthy();
});
it('should call CService.doSomething', () => {
// Spying on a mocked service
const cServiceInstance = TestBed.get(MyCService);
const spy = spyOn(cServiceInstance, 'doSomething');
app.methodWhichCallsCServiceDoSomething();
expect(spy).toHaveBeenCalled();
});
});
```