I have a simple .net core console project in VS2019 as follows.
public class Program
{
public static int Sum(int x, int y)
{
return x + y;
}
public static void Main(string[] args)
{
Func<int, int, int> sum = Sum;
Console.WriteLine(sum(10, 10));
}
}
My question is how to create a unit test project to test main().
The static void method is that you can only verify if it interacts with the input argument or static members. A better approach is to delegate all logic to separate component and test it instead. Then it doesn't matter which type of project it is. You can just create a Unit Test project and test your code.
public static void Main(string[] args)
{
var component = new Component();
component.Execute(args);
}
Example component:
public class Component
{
public static int Sum(int x, int y)
=> x + y;
public void Execute(string[] args)
{
Func<int, int, int> sum = Sum;
Console.Write(sum(10, 10));
}
}
Test:
[Fact]
public void Test()
{
//assert
var stringWriter = new StringWriter();
Console.SetOut(stringWriter);
var component = new Component();
component.Execute(It.IsAny<string[]>());
var output = stringWriter.ToString();
Assert.Equal("20", output);
}
Related
I have a method with this signature:
public static void foo(int x, int y)
{
//do something...
}
I want to verify that this method was called exactly 2 times when x = 5 and y = 10. How can I do that using Typemock?
I gave this a go and came up with the following:
Given the class:
public class Bar
{
public static void Foo(int x, int y)
{
//do something...
Debug.WriteLine($"Method called with {x} {y}");
}
}
Your test would then look like this:
[TestClass]
public class Test
{
[TestMethod]
public void TestMethod()
{
var callCount = 0;
Isolate.WhenCalled(() => Bar.Foo(2, 10))
.WithExactArguments()
.DoInstead(context =>
{
callCount++;
context.WillCallOriginal();
});
Bar.Foo(2, 6);
Bar.Foo(2, 10);
Bar.Foo(2, 10);
Assert.AreEqual(2, callCount);
}
}
i have created a class person which is having functions add,sub,mul
and mytest where i am passing my delegate reference
and in static void main i want if
DateTime.Now.Hour<12 it should call add if DateTime.Now.Hour<20 it should call sub.
but i am getting error
'ad1' does not exist in current context
class person
{
public void add(int x,int y)
{
Console.WriteLine(x+y);
}
public void sub(int x,int y)
{
Console.WriteLine(x-y);
}
public void mul(int x,int y)
{
Console.WriteLine(x*y);
}
public void test(mydel ad1)
{
ad1(2, 3);
}
}
class Program
{
static void Main(string[] args)
{
person p = new person();
if(DateTime.Now.Hour<12)
{
mydel ad1 = p.add;
}
else if(DateTime.Now.Hour<20)
{
mydel ad1 = p.sub;
}
p.test(ad1);
}
}
ad1 is declared inside the if block in the Main method and doesn't exists outside the if scope.
I don't think you need this variable at all just call directly from within the if:
static void Main(string[] args)
{
person p = new person();
if(DateTime.Now.Hour<12)
{
p.test(p.add);
}
else if(DateTime.Now.Hour<20)
{
p.test(p.sub);
}
}
This will also ensure that when Hour > 20 nothing will be called, and you won't get a null exception.
If you do however choose to use this variable, define it before the if (and assign it with null) and ensure it's not null before you use it.
class person
{
public void add(int x, int y) => Console.WriteLine(x + y);
public void sub(int x, int y) => Console.WriteLine(x - y);
public void mul(int x, int y) => Console.WriteLine(x * y);
public void test(Action<int, int> ad1) => ad1(2, 3);
}
class Program
{
static void Main(string[] args)
{
person p = new person();
Action<int, int> action = (x, y) => {};
if (DateTime.Now.Hour < 12)
{
action = p.add;
}
else if (DateTime.Now.Hour < 20)
{
action = p.sub;
}
p.test(action);
}
}
I am new to C#. I'd be thankful if any body can show me why the following error is shown for code.
"CS5001 Program does not contain a static 'Main' method suitable for an entry point"
using System;
class test
{
void Foo(int x) { Console.WriteLine(x); }
void Foo(double x) { Console.WriteLine(x); }
void Foo(int x, float y) { Console.WriteLine(x); Console.WriteLine(y);}
void Foo(float x, int y) { Console.WriteLine(x); Console.WriteLine(y); }
void Main()
{
Foo(123); // int
Foo(123.0); // double
Foo(123, 123F); // int, float
Foo(123F, 123); // float, int
}
}
This error occurs when Main method is defined with incorrect signature. This error also occurs if Main, is defined with the wrong case, such as lower-case main.
Main must be declared as static and it must return void or int, and it
must have either no parameters or else one parameter of type string[]
Define your main method like this -
static void Main()
OR like this -
static void Main(string[] args)
Have a look at this link for more information
Your fixed code should look like this:
using System;
class test
{
static void Foo(int x) { Console.WriteLine(x); }
static void Foo(double x) { Console.WriteLine(x); }
static void Foo(int x, float y) { Console.WriteLine(x); Console.WriteLine(y); }
static void Foo(float x, int y) { Console.WriteLine(x); Console.WriteLine(y); }
static void Main()
{
Foo(123); // int
Foo(123.0); // double
Foo(123, 123F); // int, float
Foo(123F, 123); // float, int
}
}
Remove <OutputType>Exe</OutputType> in project file (.csproj)
I am having some trouble with visual studio, as it says it won't compile. I can't figure out what the problem is. it says something like it can't convert from void to bool, even though there is no 'bool'. Here is my code:
using System;
namespace ConsoleApplication14
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(myFunction(14));
}
public static void myFunction(int x)
{
return x + 2;
}
}
What is causing that compile error?
try this
class Program
{
static void Main(string[] args)
{
Console.WriteLine(myFunction(14));
}
public static int myFunction(int x)
{
return x + 2;
}
}
You are returning an Int. Void has no return type. To make this work you will need
public static int myFunction(int x)
{
return x + 2;
}
The type void indicates that a mamber does not return anything. So when your method is marked as such it should not return a value. However when you call Console.WriteLine you need a value to be printed to the console.
Furthermore your method should make a calculation and return something - in your case an int. So instead of defining your method as void let it return an int:
class Program
{
static void Main(string[] args)
{
Console.WriteLine(myFunction(14));
}
public static int myFunction(int x)
{
return x + 2;
}
}
In your myfunction method your returning some int value but myfunction return type is void in your code. Plaese change void to int and the application will work.
namespace ConsoleApplication1
{
class Program
{
public static int myFunction(int x)
{
return x + 2;
}
static void Main(string[] args)
{
Console.WriteLine(myFunction(14));
Console.ReadLine();
}
}
}
I would like to call a methodA with parameters already specified from another method.
So I want to pass the method call and arguments as a parameter to MethodB and once it's done some processing, call the MethodA with arguments specified.
I know i can pass the call as a methodB as a delegate parameter and then also pass the parameters like so :
static void Main( string[] args )
{
MethodB(1, 2, Add);
Console.ReadLine();
}
public static void Add(int i, int j)
{
Console.WriteLine(i+j);
}
static public class DelegateWithDelegateParameter
{
public static void MethodB(int param1, int param2, Action<int, int> dAction)
{
dAction(param1, param2);
}
}
Wondering, if it's possible to do this with the parameters already specified rather than having to pass parameters ParamA,ParamB to MethodB from Main already specified. Was just wondering. Hope this make sense, please let me know if you want more detail.
If I'm following what you are asking, to get what you want you need to wrap your call to Add in a delegate that holds it's parameters. This is easy to do using lamda syntax:
static void Main( string[] args )
{
MethodB(() => Add(1, 2));
Console.ReadLine();
}
public static void Add(int i, int j)
{
Console.WriteLine(i+j);
}
static public class DelegateWithDelegateParameter
{
public static void MethodB(Action dAction)
{
dAction();
}
}
The statement () => Add(1,2) creates a new Action delegate that is defined to call Add() with the parametes 1,2.
I think what you want is:
static object InvokeMethod(Delegate method, params object[] args){
return method.DynamicInvoke(args);
}
static int Add(int a, int b){
return a + b;
}
static void Test(){
Console.WriteLine(InvokeMethod(new Func<int, int, int>(Add), 5, 4));
}
You can do it through Delegate. declare a delegate first and hook your method to it like
public delegate void adddelegate(int a, int b);
class Program
{
static void Main(string[] args)
{
adddelegate adddel = new adddelegate(Add);
DelegateWithDelegateParameter.MethodB(1, 2, adddel);
Console.ReadLine();
}
public static void Add(int i, int j)
{
Console.WriteLine(i + j);
}
}
static public class DelegateWithDelegateParameter
{
public static void MethodB(int param1, int param2, adddelegate dAction)
{
dAction(param1, param2);
}
}