Modify method logic MOQ [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am new to unit testing so my questions might seem basic, apologies for that.
I am trying to figure out if there is such API in MOQ that can help me to modify a method logic.
I mean,
When calling . I want to increase lets say my "count" variable by 1 and then call the method or do something else, doesn't really matter.
There is no code yet, this is a principle question. Could not find it in the MOQ Documentation on GitHub.
Hope I was clear and you can help me with that.
Thank you!

You can't 'modify' the method logic per se, but you can replace it entirely.
When you create a mock of an interface, you aren't instantiating a specific concrete type of that interface. Instead, you are allowing your mocking framework to create a 'mock' version of that interface; an object which, by default, has no functionality but requires no work to instantiate.
The easiest way to do this is to use Moq's Setup functionality, which is explained well here:
https://github.com/Moq/moq4/wiki/Quickstart

Related

Creating using-like commands in C# [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I know that C# does not currently support this, so this is more of a meta-C# question. I have been thinking of what changes would be required in the language to allow us to create our own commands like while or using. I could create a method accepting an Action, like Task.Factory.StartNew(), but then I would need parentheses around the curly braces and the syntax would not look the same.
Would this essentially amount to allowing us to apply parameters outside of the parentheses?
i.e.
public void MyUsing (IDisposable disposable) Action body
{
try
{
body();
}
finally
{
disposable.Dispose();
}
}
Commands like for, switch and if would be even more complicated to create because of their syntax.
I recommend you to take a look at the Roslyn project (which is essentially an opensource C# compiler written in C#!)
They also have demos of how easy it is to add language features.
https://roslyn.codeplex.com/
However, take note that it is unlikely for your changes to be accepted unless you are working in the C# team. If you want to have code generated for you, you could take a look at PostSharp, it isn't the same thing as you were suggesting, but it might suit you situation anyway.
http://www.postsharp.net/
PostSharp allows you to mark methods, properties, classes etc. with Attributes which will then trigger user defined code generation functions which will run after the initial compile process. It helps in situations where you don't want to copy paste the same code pattern over and over again.

Implement your own Thread in C# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I was asked at an interview today about how I would implement my own Thread. As a hint I was told that implmenting Runnable was one thing to consider, and what are the others?
I was completely stumped but even after researching it online, I still have no idea what the answer is or if it was even a valid question. I'm leaning towards the latter.
So my question is:
What things would you need to consider if you wanted to write your own implementation of the Thread class in C#?
First up, Runnable probably refers to Action. Action is a delegate type used to store a reference to a method. You use this to tell a thread what method to start on.
I expect they only wanted you to illustrate how to;
start a thread with a delegate (that is how Runnable enters the fray).
-or-
schedule work to be performed on a worker thread. I would use a call to Task.Run(new Action(....)) for this. It is concise and modern.
I would always do the latter until instructed to detail the lower level Thread class.

Creating classes [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
This might seem like a very trivial question to more experienced c# developers but please keep in mind I'am a beginner to the c# language but getting through it very well.
However I have came to a stumbling block regarding creating c# classes. I understand the whole concept of the intrinsic details on how to create the class i.e class is like a blueprint in which you create objects from in which there data is stored in fields in which you use constructors to initialize them (or default) and behaviours are performed in the form of methods bla bla etc etc.
However the question that I have is in regards to the purpose of creating your own classes in c#. I was wondering if we create our own classes like the ones that are available in the .net framework, I.e if the .Net framework hasn't got a class that wee need, so do wee then just create our own class defining the functionality that I need.
Is this the only purpose of creating classes or is this way more focused on creating DLL's, and I am missing something ?
Any ideas tips hints or expansions would be great (in laymen's Terms).
if the .Net framework hasn't got a class that wee need, so do wee then just create our own class defining the functionality that I need.
Yes.

Can I extend all objects (not just winforms control) in .Net to have a tag property? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Assuming I was writing my own version of .Net :)
What would be the downside of such a setup?
Yes, I am talking about a new anti-pattern here to avoid creating endless tuples and EventArgs. I think such a setup would have made coding a lot cleaner.
No. The Tag property has history, it was important in VB6 and Winforms was meant to replace it. It needed to be added to make porting code relatively simple.
It is entirely unnecessary in .NET. It supports implementation inheritance, a feature that VB6 didn't have. So if you want to add extra properties then you just derive a class and add them. And you'll be able to give them a good name and a type so you don't have to cast every time you read the property. This works just as well with Winforms controls.

Cool PostSharp aspects [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm looking for interesting PostSharp aspects - anything that you found useful and wouldn't mind sharing.
The coolest aspect of it for me is that it can help me implement the single responsibility principle:
"In object-oriented programming, the single responsibility principle states that every object should have a single responsibility, and that all its services should be narrowly aligned with that responsibility."
I hope to adopt this more fully with more experience in the future, but for now, I have just started with having it built in to my logging needs:
together with Log4PostSharp, I can do:
[Log(LogLevel.Info, "Counting characters.")]
int CountCharacters(string arg) {
return arg.Length;
}
What this means is that, the responsibility of logging is ascribed elsewhere (coding wise), and injected from a separate source by PostSharp and Log4PostSharp magic.
D. Patrick Caldwell has some cool ideas on his blog.
Validate Parameters Using Attributes and PostSharp
http://dpatrickcaldwell.blogspot.com/2009/03/validate-parameters-using-attributes.html
Implmementing Coding Contracts using PostSharp.
Memoizer Attribute Using PostSharp
http://dpatrickcaldwell.blogspot.com/2009/02/memoizer-attribute-using-postsharp.html
Basically, a light-weight field-value caching mechanism.
Daft question, but isn't that the purpose of the Contributions Directory?
There is an example of automatic property-change implementation - INotifyPropertyChanged.

Categories