Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

How to do unit testing in xamarin forms?

$
0
0

I created one project using xamarin forms. There I need to do unit testing on following methods

private int _valueA;

        public int ValueA
        {
            get { return _valueA; }
            set
            {
                _valueA = value;
                OnPropertyChanged();
            }
        }

        private int _valueB;
        private int _result;

        public int ValueB
        {
            get { return _valueB; }
            set
            {
                _valueB = value;
                OnPropertyChanged();
            }
        }

        public int Result
        {
            get { return _result; }
            set
            {
                _result = value;
                OnPropertyChanged();
            }
        }

        public ICommand AddCommand
        {
            get
            {
                return new Command(() =>
                {
                    Result = _valueA + _valueB;
                });
            }
        }

        public ICommand MultiCommand
        {
            get
            {
                return new Command(() =>
                {
                    Result = _valueA * _valueB;
                });
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        } 

In the view page, I am using Binding context

<ContentPage.BindingContext>
        <vm:MathViewModels />
    </ContentPage.BindingContext>

    <StackLayout Orientation="Vertical">
        <Entry FontSize="30" Text="{Binding ValueA}" />
        <Entry FontSize="30" Text="{Binding ValueB}" />
        <Button Text="Add" Command="{Binding AddCommand}"/>
        <Button Text="Multiply" Command="{Binding MultiCommand}"/>

        <Label Text="{Binding Result}"></Label>
    </StackLayout>

After this, I added unit test project there I created methods like following

[TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void AddCommand1()
        {
            try
            {
                var vm = new MathViewModels();
                vm.ValueA = 4;
                vm.ValueB = 6;

                vm.AddCommand.Execute(null);

                Assert.AreEqual(10, "vm.Result != 10");
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        [TestMethod]
        public void MultiplyCommand1()
        {
            try
            {
                var vm = new MathViewModels();
                vm.ValueA = 4;
                vm.ValueB = 6;

                vm.MultiCommand.Execute(null);

                Assert.AreEqual(24, "vm.Result != 24");
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }

I added reference of xamrin forms project in the unit test project
When I run my test methods I am getting following error

What is the issue? How to overcome this?


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>