How to mock a Class like this in C#? [closed] - c#

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 want to mock a method in the class
public class Test<T> where T : class
{
public string GetData()
return "Test"
}
}
How to mock the GetData class by using FakeItEasy? When I try to return a value using FakeITEasy it given me an message saying parameterless constructor
An example
var FakeTest = A.Fake<Test<DocumentTest>>();
A.CallTo(() => FakeTest.GetByIdAsync("Site"(A<string>.Ignored))).MustHaveHappened();

To fix a missing parameterless constructor just add
public Test() {}
to your class shown. That allows a mocking framework to new up an instance of the class and then set properties as needed.

Related

Calling a class in another way [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 4 years ago.
Improve this question
I am just a beginner and I am trying to learn other alternatives.
Is there another way to call a class from another class. For example, I have a class called Test, can you call it in another way from this one = Test example = new Test();
You can have a Type Factory class which can create an instance for you dynamically. Read some articles about type factory.
Its a very common way to dynamically create instances of a type.
Lets say you have a program that generates text files. As parameter you can ask for a specific template, the program should then browse through your classes of type text generator and create an instance matching your requested template.
If you need to create an instance of this class i.e this is not a static class then
Test example = new Test();
is the correct way to call this class.
If this was a static class e.g public static Test {... then you could call this class without creating a new instance of it e.g Test.SomeMethod Example Link

What is the difference between PrivateObject vs PrivateType 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 5 years ago.
Improve this question
I was looking for approaches that could be used to access a static class which contains private static methods to unit test.
I came across PrivateObject and PrivateType, I do not understand what the difference between them is, and how you would decided to use either one.
BusinessLogic
public static Class BarLogic(){
private static void FooBar(){
//do something here
}
}
API
public class FooService(){
BarLogic.FooBar();
}
The difference is whether you're accessing otherwise inaccessible instance members or static members.
In the example below, you would use PrivateObject to access _someInstanceField, and PrivateType to access _someStaticField.
public class SomeObjectWithInAccessibleMembers
{
private int _someInstanceField;
private static int _someStaticField;
}
Given that your test was in a separate assembly from this class (and therefore unable to to see its internal members) you could do this:
var privateObject = new PrivateObject(new SomeObjectWithInAccessibleMembers());
privateObject.SetField("_someInstanceField", 1);
Assert.AreEqual(1, privateObject.GetField("_someInstanceField"));
var privateType = new PrivateType(typeof(SomeObjectWithInAccessibleMembers));
privateType.SetStaticField("_someStaticField", 1);
Assert.AreEqual(1, privateType.GetStaticField("_someStaticField"));
I've never used this. I didn't even know about it. My first reaction is that it couples the test to the inner implementation of the class instead of testing its public methods. Then it couples it some more by using strings for member names, so the tests could break just by changing a member name.
On the other hand, I've worked in projects where a) I want to add unit tests, but b) I can't refactor to make it more testable. It's not my first choice but I might have a use for this. Thanks!

Casting a Generic Class<T> in C# from an Interface? [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 have a generic class based on an Interface
public class SomeClass<T>() : ISomeClass
{...}
and in it, I have a public method, which is an implementation of the ISomeClass interface and returns a SomeClass<T> type.
public SomeClass<T> SomeMethod()
{...}
Then I declare a variable somewhere else, based on the interface
private ISomeClass _someVariable;
Now, if I instantiate the class...
_someVariable = new SomeClass<SomeType>();
... I can access my public method
var anotherVariable = _someVariable.SomeMethod();
However, it returns an ISomeClass type and not a SomeClass<T>type. Why? Is this because I declare _someVariable as ISomeClass? (I've tried casting it as SomeType<T> as well, but still get a type ISomeClass.) What am I missing? (other than a whiskey and some sleep?)
if someVariable is ISomeClass (which it is), then the relevant question is: what type does ISomeClass.SomeMethod() return, because that is the definition being used; I'm guessing it returns ISomeClass, which is why anotherVariable is typed as ISomeClass.
To use the SomeClass<T>.SomeMethod() signature that returns a SomeClass<T>, _someVariable would need to be known as a SomeClass<T>, not just an ISomeClass.
Note that your SomeClass<T>.SomeMethod() method won't actually satisfy that interface, so you probably also have an explicit interface implementation somewhere:
ISomeClass ISomeClass.SomeMethod() => SomeMethod();
(which highlights the same problem)

C# - Cast instance to generic interface returns null [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 6 years ago.
Improve this question
I have the following method:
IResponseToModelConverter<U, IEntity> CreateConverter()
{
return new ResponseToItemConverter() as IResponseToModelConverter<U, IEntity>;
}
ResponseToItemConverter inherits from a base class that implements IResponseToModelConverter.
Item is type of IEntity, and I can get the type of U.
Is there a way for this to work?
I replaced IEntity with type parameter T (with IEntity constraint) so I can pass a concrete class when I cast to the interface. That solved my problem.

Unexpected character error in C# dot net application [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a solution created in C#. i have a piece of code in it base.\u002Ector();. When i try to build the application from Visual Studio I am getting the Following error.
Unexpected character '\u002E'
Remove \u002E and you're done!
002E is a full stop or just a dot ('.').
Your code must look like:
base.ctor();
It could happen because of transmitting a program source through different websites/programs with unsupported or broken encoding.
Moreover, ctor is a shortcut for constructor. Make sure you call constructor of base class correctly.
public class Animal
{
public Animal()
{
}
}
public class Cat : Animal
{
// next line will be converted to
// base.ctor() by compiler
public class Cat() : base()
{
}
}
Try to remove base.ctor(); in order to make your class work. I think it should work because your base class has no parameters in constructor.

Categories