【问题标题】:How can I mock an update of an object in an array : Unit testing Angular如何模拟数组中对象的更新:Angular Unit testing
【发布时间】:2018-05-29 14:29:10
【问题描述】:

我正在单元测试我的 redux reducers,但到目前为止我已经测试了 GETCREATE我不知道如何进行 UPDATE。我不知道如何嘲笑它。我还在下面的 reducer 中添加了 UPDATE 的代码。

这是我迄今为止在测试中所做的:

describe('register reducer', () => {
    let initialState;
    beforeEach(() => {
        initialState = UsersService.getInitialUsersState();
        deepFreeze(initialState);
    })

    it('should return the initial state', () => {
        expect(usersReducer(undefined, [])).toEqual(initialState);
    });
    it('Should add a new baby object to the babies array', () => {
        // create 'mock' of initial state
        // add baby by calling reducer function
        // check that state is correct. Use deep freeze to check no mutations.
        let afterState = UsersService.getInitialUsersState();
        let baby = {
            firstname: 'Peter',
            lastname: 'Petursson',
            username: 'test baby 1',
            birthDate: new Date(2018, 0, 1),
            area: 'Copenhagen',
            rating: []
        };
        afterState.babies.push(baby);

        let newState = usersReducer(initialState, {
            type: types.UsersActions.CREATED_BABY,
            payload: baby
        });
        expect(newState.babies.length).toEqual(1);
        expect(newState).toEqual(afterState);
    });
    it('Should get the babies array', () => {
        let afterState = UsersService.getInitialUsersState();
        let babies = [{
            _id: "5ad228ffdc32f1a42f5d3259",
            firstname: "Oliver",
            lastname: "Hultgren",
            username: "o@h",
            birthDate: new Date(2018, 0, 1),
            area: "København Ø",
            rating: [],
            userType: "baby",
            dataClient: "Julia"
        },
        {
            _id: "5ad229d7dc32f1a42f5d325a",
            firstname: "Oli",
            lastname: "Hult",
            username: "o.h@dk",
            birthDate: new Date(2018, 0, 1),
            area: "København V",
            rating: [],
            userType: "baby",
            dataClient: "Julia",
        }, {
            _id: "5ad309337cbda3002a7e92e0",
            firstname: "Jenny",
            lastname: "Lopez",
            username: "jl@com",
            birthDate: new Date(2018, 0, 1),
            area: "København Ø",
            rating: [],
            userType: "baby",
            dataClient: "Julia",
        }]
        afterState.babies = babies;
        let newState = usersReducer(initialState, {
            type: types.UsersActions.RECEIVED_BABIES,
            payload: babies

        });
        expect(newState).toEqual(afterState);
    })

//更新到这里

it('Should update a baby object in the babies array', () => {}}

这就是我的实际减速器的样子:

 case UsersActions.UPDATED_BABY:
            let indexBaby = state.babies.findIndex(baby => { return baby._id === action.payload._id });
            return tassign(state, { babies: [...state.babies.slice(0, indexBaby), action.payload, ...state.babies.slice(indexBaby + 1)] });

【问题讨论】:

  • 您可以对婴儿[0] 进行测试,更改它,发送更新操作,然后将结果与您发送的有效负载进行比较
  • 你能给我看一下代码吗?

标签: angular unit-testing mocking updates


【解决方案1】:

试试这个

 it('Should add a new baby object to the babies array', () => {
    let afterState = UsersService.getInitialUsersState();
     let babies = [{
        _id: "5ad228ffdc32f1a42f5d3259",
        firstname: "Oliver",
        lastname: "Hultgren",
        username: "o@h",
        birthDate: new Date(2018, 0, 1),
        area: "København Ø",
        rating: [],
        userType: "baby",
        dataClient: "Julia"
    },
    {
        _id: "5ad229d7dc32f1a42f5d325a",
        firstname: "Oli",
        lastname: "Hult",
        username: "o.h@dk",
        birthDate: new Date(2018, 0, 1),
        area: "København V",
        rating: [],
        userType: "baby",
        dataClient: "Julia",
    }];
    let updatedBaby = {
        _id: "5ad229d7dc32f1a42f5d325a",
        firstname: "ChangedName", // change the name for example
        lastname: "Hult",
        username: "o.h@dk",
        birthDate: new Date(2018, 0, 1),
        area: "København V",
        rating: [],
        userType: "baby",
        dataClient: "Julia",
    }
    let newState = usersReducer(initialState, {
        type: types.UsersActions.UPDATED_BABY,
        payload: updatedBaby

    });
    expect(newState[1].firstname).toEqual('ChangedName'); // to check that update is working and that you have the new firstname

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-25
    • 2018-11-21
    • 1970-01-01
    • 2012-04-06
    • 2019-11-01
    • 2020-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多