Need a great explanation of using MOQ syntax [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
I seem to have problems getting MOQ syntax by example. I would like to have suggestions where to look, besides MOQ website, to comprehend usage of mocking various interfaces.
For example, I would like to understand how exactly I proceed mocking this or that interface method and be able, for example, to undestand why a developer writes things like this:
daoMock.Setup(d => d.Get(It.Is<Expression<Func<ConfigurationEntity, bool>>>(e => ExpressionMatchesEntityWithKey(e, TestKey))))
.Returns(new List<ConfigurationEntity> {configEntity});
thanks in advance!

The github page, as suggested by #Jakub, is actually a great place to start.
Either way, I'll explain the example you posted.
Say you have this interface and this setup:
public interface IDao
{
IEnumerable<ConfigurationEntity> Get(Expression<Func<ConfigurationEntity, bool>> expression) {...}
}
var daoMock = new Mock<IDao>();
daoMock.Setup(d => d.Get(It.Is<Expression<Func<ConfigurationEntity, bool>>>(e => ExpressionMatchesEntityWithKey(e, TestKey))))
.Returns(new List<ConfigurationEntity> {configEntity});
This setup tells the mock to return a list containing configEntity when:
the Get method is called
with an argument of type Expression<Func<ConfigurationEntity, bool>>
and ExpressionMatchesEntityWithKey(e, TestKey) (where e is the expression passed in as an argument) returns true.
So, basically, when you call the Get method on the mocked object, Moq will check if the argument is of the correct type, check if ExpressionMatchesEntityWithKey(e, TestKey) returns true, and then return a list with configEntity.
By default, if any of these requirements are not met, the mocked object will return the default value for IEnumerable<ConfigurationEntity>, which is null.
Now, you can retrieve the mocked object and do whatever you want with it.
IDao dao = daoMock.Object;

Related

How to pass a var type to a method C# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I 've a doubt that researching I couldn't solve.
I want to pass a parameter using a var type to this method:
public static List<T[]> sortRequests<T>(List<T> _requests)
{
return null;
}
And I am calliinf the method here:
//Note: Thats code corresponds from an HttpRest service.
var _requests = await _requestService.Search(new SearchRequestSpecificationMapper().Map(searchRequestsViewModel));
var requests = sortRequests(_requests);
So VS 2015 is reporting about an error when I call the sortRequest() method.
Can anybody help me please?
Thank you very much in advance.
_requests is (presumably) not a List<T>.
You might be able to make it into a List<T> by caling ToList:
var requests = sortRequests(_requests.ToList());
However, I can't be certain without knowing more about your code, especially what the Search and Map functions do.
NOTE
Please be aware that there is no such thing as a var type in C#. var is just a bit of syntactic sugar that means "This variable has the type of whatever I assign to it when I declare it" - it's no different to just declaring it as the correct type.

Parameter Info Tooltip using Roslyn [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this question
I would like to develop my own tooltip for Parameter Info (which pops up as soon as you start entering parameters in a function call).
I would like to implement it using Roslyn, but I don't know where to start. Can anyone provide me a small example to get me started?
I should preface this by pointing out that extending Visual Studio is not a particularly easy, fun or straightforward endeavor.
I believe the MSDN article Walkthrough: Displaying Signature Help should get you off to a good start.
The Signature Help source is based on signatures that implement ISignature, each of which contains parameters that implement IParameter.
So first we have to create a parameter that inherits from IParameter.
Next we have to create a signature that inherits from ISignature. The key here is to implement a CurrentParameterChanged event that is fired as the user types commas, changing with parameter's definition should be shown.
This is accomplished by creating the event and firing it as follows:
public event EventHandler<CurrentParameterChangedEventArgs> CurrentParameterChanged;
public IParameter CurrentParameter
{
get { return m_currentParameter; }
internal set
{
if (m_currentParameter != value)
{
IParameter prevCurrentParameter = m_currentParameter;
m_currentParameter = value;
this.RaiseCurrentParameterChanged(prevCurrentParameter, m_currentParameter);
}
}
}
private void RaiseCurrentParameterChanged(IParameter prevCurrentParameter, IParameter newCurrentParameter)
{
EventHandler<CurrentParameterChangedEventArgs> tempHandler = this.CurrentParameterChanged;
if (tempHandler != null)
{
tempHandler(this, new CurrentParameterChangedEventArgs(prevCurrentParameter, newCurrentParameter));
}
}
They computer the current parameter based on the number of commas in the string. The ComputeCurrentParameter() method is a little too long to post here, however.
Next you have to implement ISignatureHelpSource. This interface provides signature help information for Intellisense.
The method ISignatureHelpSource.AugmentSignatureHelpSession() is where the list of parameter information is created and where you'll be adding your custom parameter information. The MSDN provided example uses pre-written strings here. In reality, you'd probably want to calculate these things on the fly, perhaps with Roslyn, depending on your goals.
Finally, you must export an ISignatureHelpSourceProvider via MEF. This allows Visual Studio to consume your ISignatureHelpSource.

Web service WHERE / ORDER BY (?) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a web service that returns the list of all the clients.
I can get the list into a gridview by calling the method get all data like this.
test.RH_WebServiceService ligar = new test.RH_WebService();
test.baseList[] data = ligar.getAllData();
The thing is I wanted to filter it by name (for example) I've been reading online and people have said to me that I can just do it like this:
test.baseList[] data = ligar.getAllData().Where(condition);
However I can't get it to work. Do you guys have any ideas?
Assuming you are using Linq then you can just do:
test.baseList[] data = ligar.getAllData().Where(d => d.Name == "John");
The d is a random letter given to the object. Name is what i am assuming your property is called. Although i would recommend creating a method in your service that you pass the name in and get back the filtered data. That way you only return data you need which will improve performance. Something like this:
test.baseList[] data = ligar.getDataByName("John");
Maybe this will help some:
test.baseList[] data = ligar.getAllData().Where(f=> f.field == "value").ToArray();

Cant create an object inside main [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an abstract class which name is abclass,
why i cant create an object like
abclass ab1 = new abclass();
from main
I have constructor in abclass also.
You cannot create an instance of an abstract class.
If you don't provide an implementation for some methods on your abstract class, it cannot, obviously, work.
I suggest you Google 'abstract classes' for more information.
If you want to instantiate your class, remove the abstract keyword.
Read about "Abstract Classes" in MSDN
It is beautifully explained !!!!
The reason is: Abstract class can not be instantiated .
Lets do some basics here:
check http://msdn.microsoft.com/en-us/library/ms173150.aspx

Can I check if class implements an interface when it is casted to other interface [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Is it possible to check interface implementation in this way?
class ProjectSettingsApplier : ISettingsApplier, IApplyChanges
{
}
ISettingsApplier applier = new ProjectSettingsApplier()
if(applier is IApplyChanges)
{
//Is it true??
}
From the documentation for is:
An is expression evaluates to true if the provided expression is
non-null, and the provided object can be cast to the provided type
without causing an exception to be thrown. Otherwise, the expression
evaluates to false.
So I would say yes, that would work.
That said, a simple repro would likely answer this for you.
Yes, that is valid, and looks like a good way to do the check.
If all IApplyChanges are also ISettingsAppliers, you should make IApplyChanges : ISettingsApplier, to make the relationship clearer.
Yes, it is legal since applier is in fact implementing IApplyChanges. In your case, it will the evaluation will be true.
Alternatively, you can use IsAssignableFrom but it's way more chatty and you have to make sure applier is not null:
typeof(IApplyChanges).IsAssignableFrom(applier.GetType());
That's perfectly OK, but if you want to go on to cast the interface to IApplyChanges to use it, you should use as instead of is like so:
ISettingsApplier applier = new ProjectSettingsApplier()
var changer = applier as IApplyChanges;
if (changer != null) // Will only be non-null if it implements IApplyChanges
{
// Use changer.
}
Doing it this way avoids a double type check, which is not as efficient. In other words, don't do it like this:
ISettingsApplier applier = new ProjectSettingsApplier()
if (applier is IApplyChanges) // Type check #1
{
var changer = (IApplyChanges) applier; // Type check #2: Inefficient.
// Use changer.
}

Categories