from I select q[0]
from I select q[0]
CC#
Created by from I select q[0] on 7/17/2024 in #help
help understanding unit test and mocking, which of the two example are valid
public interface ILocalServiceWraperTest
{
IThirdPartyService service { get; set; }

void SetupService();
(string, int) TransformDoSomething(string a, string b);
}
public class LocalServiceWraper : ILocalServiceWraper
{
public IThirdPartyService service { get; set; }

public void SetupService()
{
service = ServiceLoaderExample();
throw new NotImplementedException();
}

public (string, int) TransformDoSomething(string a, string b)
{
int res = service.GiveMeSomething(a, b, out string[] resultValues);
if (res == 0) return ("somethng", res);
return ("nothing", -1);
}
}
//TEST
[TestMethod]
public void TestMethod1()
{
var thirdPservicemock = new Mock<IThirdPartyService>();
var localSericeMock = new Mock<ILocalServiceWraper>();
var strarr = new string[] { "a", "b", "c" };
localSericeMock.Object.service = thirdPservicemock.Object;
localSericeMock.Setup(x => x.SetupService());

thirdPservicemock.Setup(x => x.GiveMeSomething(It.IsAny<string>(), It.IsAny<string>(), out strarr));

var res = localSericeMock.Object.TransformDoSomething("a", "b");


Assert.AreEqual(-1, res.Item2);
}

//
[TestMethod]
public void TestMethod2()
{
var thirdPservicemock = new Mock<IThirdPartyService>();
var localSerice = new LocalServiceWraper(); //using concreate imple
var strarr = new string[] { "a", "b", "c" };
localSerice.service = thirdPservicemock.Object;
localSerice.SetupService();

thirdPservicemock.Setup(x => x.GiveMeSomething(It.IsAny<string>(), It.IsAny<string>(), out strarr));

var res = localSerice.TransformDoSomething("a", "b");


Assert.AreEqual(-1, res.Item2);

}
public interface ILocalServiceWraperTest
{
IThirdPartyService service { get; set; }

void SetupService();
(string, int) TransformDoSomething(string a, string b);
}
public class LocalServiceWraper : ILocalServiceWraper
{
public IThirdPartyService service { get; set; }

public void SetupService()
{
service = ServiceLoaderExample();
throw new NotImplementedException();
}

public (string, int) TransformDoSomething(string a, string b)
{
int res = service.GiveMeSomething(a, b, out string[] resultValues);
if (res == 0) return ("somethng", res);
return ("nothing", -1);
}
}
//TEST
[TestMethod]
public void TestMethod1()
{
var thirdPservicemock = new Mock<IThirdPartyService>();
var localSericeMock = new Mock<ILocalServiceWraper>();
var strarr = new string[] { "a", "b", "c" };
localSericeMock.Object.service = thirdPservicemock.Object;
localSericeMock.Setup(x => x.SetupService());

thirdPservicemock.Setup(x => x.GiveMeSomething(It.IsAny<string>(), It.IsAny<string>(), out strarr));

var res = localSericeMock.Object.TransformDoSomething("a", "b");


Assert.AreEqual(-1, res.Item2);
}

//
[TestMethod]
public void TestMethod2()
{
var thirdPservicemock = new Mock<IThirdPartyService>();
var localSerice = new LocalServiceWraper(); //using concreate imple
var strarr = new string[] { "a", "b", "c" };
localSerice.service = thirdPservicemock.Object;
localSerice.SetupService();

thirdPservicemock.Setup(x => x.GiveMeSomething(It.IsAny<string>(), It.IsAny<string>(), out strarr));

var res = localSerice.TransformDoSomething("a", "b");


Assert.AreEqual(-1, res.Item2);

}
10 replies
CC#
Created by from I select q[0] on 8/31/2023 in #help
✅ unit testing ViemModels
I am very new to unit testing, and I have a typical example where I have a constructor with 4 services 3 of which is DI and one concrete generic for navigation E.g public
cs class ViewModel {

Public ViewModel ( IB service, IC service, ID service, E<F> navigation) {

}

}
cs class ViewModel {

Public ViewModel ( IB service, IC service, ID service, E<F> navigation) {

}

}
I have be having problem testing because I need to create a moq of E<F> and because of the way it is setup it's hard to abstract to interface. Does it make sense to create IViewModel And then have Class ViewModel:IViewModel And mock IViewModel instead? Is this the recommended approach? Is this good or bad ?
9 replies
CC#
Created by from I select q[0] on 4/14/2023 in #help
❔ trying to get published events in one project to the other
I have a Solution with two projects 1. Solution 1a. Project ( winUI3) 1b. Project.core 1a references 1b(core project) --- i am trying to publish events in 1b(core projects) and subscribe to the events in 1a(Ui projects viewmodel)
2 replies
CC#
Created by from I select q[0] on 2/17/2023 in #help
❔ unable to send messages using MVVMtoolkit messenger
i am trying to pass messages between two viewmodels employeeviewmode and storeviewmodebut it doesn't work
public class StoreViewModel :BaseViewModel
{


public ICommand NavigateEmployeeCommand { get; }
public StoreViewModel(INavigation storeNav)
{
NavigateEmployeeCommand = new NavigateCommand(storeNav);
}


protected override void OnActivated()
{
Messenger.Register<StoreViewModel, MonthNameMessage>(this, (r, m) => r.MonthMessageReceived= m.Value

);
}

private string _monthMessageReceived;
public string MonthMessageReceived { get => _monthMessageReceived;

set => SetProperty(ref _monthMessageReceived, value); }

}

//employee viemodel
public class EmployeeViewModel:BaseViewModel
{

public ICommand NavigateStoreCommand { get; }

public ICommand SendMessageCommand { get; }
public EmployeeViewModel(INavigation storeNav)
{
NavigateStoreCommand = new NavigateCommand(storeNav);

SendMessageCommand= new RelayCommand(SendMessage);

}


private readonly string _testVariable = "Employee haahahah";
public string TestVariable
{
get => _testVariable;


}

protected override void OnActivated()
{
Messenger.Register<EmployeeViewModel, CurrentMonthRequestMessage>(this, (r,m)=> m.Reply(r._testVariable));
}


public void SendMessage()
{
Messenger.Send(new MonthNameMessage(_testVariable));
}

}

//Message classes
public sealed class MonthNameMessage : ValueChangedMessage<string>
{
public MonthNameMessage(string value) : base(value)
{
}
}
public sealed class CurrentMonthRequestMessage:RequestMessage<string>
{

}
public class StoreViewModel :BaseViewModel
{


public ICommand NavigateEmployeeCommand { get; }
public StoreViewModel(INavigation storeNav)
{
NavigateEmployeeCommand = new NavigateCommand(storeNav);
}


protected override void OnActivated()
{
Messenger.Register<StoreViewModel, MonthNameMessage>(this, (r, m) => r.MonthMessageReceived= m.Value

);
}

private string _monthMessageReceived;
public string MonthMessageReceived { get => _monthMessageReceived;

set => SetProperty(ref _monthMessageReceived, value); }

}

//employee viemodel
public class EmployeeViewModel:BaseViewModel
{

public ICommand NavigateStoreCommand { get; }

public ICommand SendMessageCommand { get; }
public EmployeeViewModel(INavigation storeNav)
{
NavigateStoreCommand = new NavigateCommand(storeNav);

SendMessageCommand= new RelayCommand(SendMessage);

}


private readonly string _testVariable = "Employee haahahah";
public string TestVariable
{
get => _testVariable;


}

protected override void OnActivated()
{
Messenger.Register<EmployeeViewModel, CurrentMonthRequestMessage>(this, (r,m)=> m.Reply(r._testVariable));
}


public void SendMessage()
{
Messenger.Send(new MonthNameMessage(_testVariable));
}

}

//Message classes
public sealed class MonthNameMessage : ValueChangedMessage<string>
{
public MonthNameMessage(string value) : base(value)
{
}
}
public sealed class CurrentMonthRequestMessage:RequestMessage<string>
{

}
2 replies
CC#
Created by from I select q[0] on 10/7/2022 in #help
can anybody recommend where I can learn about file format encoding internals e.g pdf, doc, epubs
can anybody recommend where I can learn about file format encoding internals e.g pdf, doc, epubs
6 replies