jeudi 26 décembre 2019

Mocking of Where method in Data Repository is returning null at its actual implementation in repository

I have written a test for edit model of a class in EF core.

public async Task<Expense> EditExpense(Expense model)
{
        var expense = _dataRepository.Where<Expense>(x => x.Id == model.Id).ToList();

        if(expense != null)
        {
            expense.ForEach(x =>
            {
                x.Name = model.Name;
                x.AddedOn = model.AddedOn;
                x.Amount = model.Amount;
                x.Type = model.Type;
                x.SplitOption = model.SplitOption;
                x.Notes = model.Notes;
                x.IsGroupExpense = model.IsGroupExpense;
            });

            return expense.FirstOrDefault();
        }
        else
        {
            return null;
        }
}

I want to test this method using xUnit and Moq and I have also written a method for it as below.

[Fact]
public async void UnitTest_IsExpenseEdited_ReturnsTrue()
{
        var expenseCurrent = new Expense()
        {
            Id = 5,
            Name = "Test Expense",
            Amount = 1500,
            AddedBy = "44db32c3-ad6a-4d68-a683-862be363f59c",
            AddedOn = DateTime.Now,
            Notes = "",
            IsDeleted = false,
            IsGroupExpense = false,
            SplitOption = 1,
            Type = 0
        };

        var expenseList = (new List<Expense>{ expenseCurrent });

        var expenseAfterEdit = new Expense()
        {
            Id = 5,
            Name = "Test Expense Edited",
            Amount = 2000,
            AddedBy = "44db32c3-ad6a-4d68-a683-862be363f59c",
            AddedOn = DateTime.Now,
            Notes = "Edited !!!",
            IsDeleted = false,
            IsGroupExpense = true,
            SplitOption = 2,
            Type = 0
        };

        _dataRepositoryMock.Setup(x => x.Where<Expense>(a => a.Id == expenseCurrent.Id)).Returns(expenseList as IQueryable<Expense>);

        var expenseEdited = await _exepenseRepository.EditExpense(expenseAfterEdit);
        Assert.Equal(expenseEdited, expenseAfterEdit);
}

But here, the Where method is returning null

public async Task<Expense> EditExpense(Expense model)
{
        var expense = _dataRepository.Where<Expense>(x => x.Id == model.Id).ToList(); in repository 
}

I am getting var expense null in above code.

Please suggest what should I include in the code to get the value in above var expense?

To test this method, I want to create a fake data which is going to be updated. Please suggest how this thing needs to be written properly?

mercredi 25 décembre 2019

Is it possible to make tests for grpc like for rest?

is it possible to make tests for grpc, like it can be done for post/get requests with requests module in python ? I have seen python modules like pytest-grpc, tavern-grpc, but i don't have understanding of how to use it to make request and check response. Thanks !

About testing HOC component

I have a component wrapped by withStyles and withTranslation HOC, now I wanna do a shallow testing the component but I don't know how to get the wrapped component. Just a simple test: all fields should be rendered, so I don't wanna use mount for this, and I can't make export default because it had a default one. This is my code:

import React, { Component } from 'react';
import { withStyles, Grid, TextField, Button } from '@material-ui/core';
import { createShallow } from '@material-ui/core/test-utils';
import { withTranslation } from 'react-i18next';
import { styles } from '.MyStyles';

export const MyComponent = withTranslation(withStyles(styles)(class extends Component {
  constructor(props) {
    super(props);
    this.state = {
      isWrong: false,
      isEmpty: false,
      value: '',
    }
    onChange = (e) => {
      const { value } = this.state;
      e.preventDefault();
      this.setState({value: e.targer.value});
      value==''?this.setState({isEmpty: true}):this.setState({isEmpty: false});
    }
    onClick = () => {
      const { isEmpty } = this.state;
      isEmpty?this.setState({isWrong: true}):this.setState({isWrong: false});
    }
  }
  render() {
    const { classes, t } = this.props;
    const { isWrong, value } = this.state;
    return (
      <div>
        <Grid container className={classes.content1}>
          <Grid item>{t('Task 1')}</Grid>
          <Grid item>{t('Task 2')}</Grid>
        </Grid>
        <Grid container className={classes.content2}>
          <div>{t('Task 3')}</div>
          <TextField label={t('Info')} value={value} error={isWrong} onChange={this.onChange}></TextField>
          <Button variant="contained" onClick={this.onClick}>{t('Change')}</Button>
        </Grid>
      </div>
    );
  }
}));

{...}

and this is my test:

describe("check", () => {
  let shallow;
  let wrapper;
  beforeEach(() => {
    shallow = createShallow();
    wrapper = shallow(<MyComponent />);
  });
  it("check component render right", () => {
    const div = wrapper.find('div');
    expect(div).toMatchSnapshot();
    expect(div).toHaveLength(1);
  });
});

How to test text similarities approaches?

My goal is to benchmark different text similarities approaches for detecting short sentences duplicates. I have applied different methods, simple edit distances and also semantic text similarities techniques. I have prepared a golden dataset that consists of 500 text pairs that I consider duplicates. When I apply different text similarities I get scores between 0 and 1 for each pair. I am confused about what metrics to use to compare the performance of these techniques and what threshold score to use for classifying as duplicate and as not duplicate.

Thanks in advance!

What could be the reasons for taking more time while doing automation testing than manual testing for a particular web page for suppose?

What could be the reasons for taking more time while doing automation testing than manual testing for a particular web page for example???

Testing queston function

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin non lacus sollicitudin, tincidunt nibh non, posuere odio. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Donec in consectetur lorem, ut tristique tortor. In cursus dapibus risus nec congue. Donec maximus tellus dui, eu fringilla ante feugiat eu. Sed in auctor velit. Integer interdum laoreet est eget dignissim. Nam ultricies consectetur condimentum. Praesent id mauris efficitur, malesuada mi vitae, congue justo. Aenean malesuada porta nisl sed volutpat. Mauris et tincidunt augue. Suspendisse at metus pretium, feugiat ex at, blandit tortor.

Unresolved symbols for androidTest dependencies in case of non-standard build types

When I've decided to add instrumentation tests, I found that androidTestImplementation dependencies are not recognized. In my test file related classes like RunWith were displayed as Unresolved symbols. When I've tried to run the test anyway, in run configuration there was a message: Error: Instrumentation runner class not specified.". I've checked, instrumentation runner class was explicitly specified in build.gradle.

I use custom build types like debugDev, debugProd.