Unit Testing with ForEach [closed] - c#

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 7 years ago.
Improve this question
Assuming:
"personList" is a list of Person objects
"agent" is an object that can provide activity about Shop objects (a property of Person)
"GetShopActivity" returns a list of shopping activity objects for a Person
I have this line in a test project:
personList.ForEach(p => new List<Person>(p.Shops)
.ForEach(t=> Assert.IsNotNull(agent.GetShopActivity(t, startDate, endDate))));
How can I make it better?

There is probably a lot you could do, but the first thing to do would be to make it more readable. Perhaps something like this:
var nullActivities =
from p in partnerList
from t in p.Tenants
let activity = agent.GetShopActivity(t, startDate, endDate)
where activity == null
select activity;
Assert.Empty(nullActivities);
Moreover:
you may want to think about
a test should be simple (i.e. it should have a Cyclomatic Complexity of 1).
it should be immediately evident to a person reading the test what scenario and behaviour is being tested (in case the values of startDate and endDate are significant it might be beneficial to give them less generic names).
prefer having only a single assertion as that makes it easy to know where the test failed when it fails.

Related

Dealing with multiple booleans [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 2 days ago.
This post was edited and submitted for review 2 days ago.
Improve this question
I have a Customer data object. That object has to be evaluated by different functions. Each function will return true or false.
Since each function evaluate different case scenarios, I need to submit the Customer object to each one of them.
The way the functions were written, only one of them is going to return true.
(The functions are used in another part of the code, so I am using them as it is, so I don't have to repeat code).
The WorkStatus is an Enum. So, if the function returns true, then the Enum will be set with the specified value.
So:
WorkStatus result = 0;
if (notStarted) result = WorkStatus.NotStarted;
if (started) result = WorkStatus.Started;
if (inProgress) result = WorkStatus.InProgress;
if (withBacklog) result = WorkStatus.WithBacklog;
if (finalized) result = WorkStatus.Finalized;
return (int)result;
I made this way, but I am sure there is some better way. I just can't figure this out.
Obs.: I wrote the question again. I hope it's more clear. Sorry for the previous lack of details.

Simple foreach, preferred way [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 12 months ago.
Improve this question
I'm accessing some values from the list to populate list of strings in this way
List<string> carNames = null;
foreach (var car in dal.Cars)
{
carNames.Add(car.Name);
}
Is there more efficient way, yet readable to write this? How would you do it?
"Is there a more efficient way"
More efficient is often micro optimization and most times decreases readability. However, one way to optimize performance would be to initialize the list with the correct capacity, which you know already because dal.Cars is also a list:
List<string> carNames = new List<string>(dal.Cars.Count);
carNames.AddRange(dal.Cars.Select(c => c.Name));
Sidenote: i really wonder why microsoft didn't add an overload for ToList/ToArray/ToHashSet which takes the capacity. Then you could use this (Does NOT compile):
List<string> carNames = dal.Cars.Select(c => c.Name).ToList(dal.Cars.Count);

Selecting the default rule [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 2 years ago.
Improve this question
I'm trying to find the name of what I want to achieve. We need to build a rules engine with some fallback or fail over logic.
When the request is JP, the first rule should be selected; for any other country, it should pick up the second one. I don't want to put an entry for all the countries we'll be dealing with, hence the '*'.
What is this called? How is this built in C#?
Thanks,
Arun
My guess that you are looking for a flavor of rule engine implementation for C#
One solution would be to use NRules library.
You can easily create any rule you want
public class DiscountNotificationRule : Rule
{
public override void Define()
{
Customer customer = null;
When()
.Match<Customer>(() => customer)
.Exists<Order>(o => o.Customer == customer, o => o.PercentDiscount > 0.0);
Then()
.Do(_ => customer.NotifyAboutDiscount());
}
}
Extra information in the getting started page of the library.

what is best way to prepare C# objects in separate methods [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
What is better method to use for object preparation logic:
a) with return value
List<Users> users = LoadUsers();
users = PrepareUsers(users);
b) or with void type
List<Users> users = LoadUsers();
PrepareUsers(users)
Are you setting properties on existing User objects or are you creating new ones?
If you're simply changing existing objects, then there's no reason why you'd want to return them, it's redundant. Worse, it's misleading - the client will think his objects were left untouched and that you're creating new objects when in fact you're not.
If you're creating new ones, well then, you obviously need to return them.
Alternative b. since you are working with the same user objects, there is no reason to reassign the variable.

MSTest - why is there no AreEqual(object, object, IComparer<T>)? [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
Writing UnitTests with MSTest I want to assert the equality of a return value vs. the one I'm expecting.
Expected type is a custom type that does not implement the IComparable interface nor the IEquatable interface, thats why I want to give Assert.AreEqual a possibility to compare the two objects.
I am aware that this possibility exists in CollectionAssert.AreEqual. This method however requires two ojects that inherit ICollection which my objects do not.
Why does Assert.AreEqual not allow me to specify a custom comparer? Am I missing something?
Not sure if this is the actual reason, but what if your custom IComparer was faulty - your unit test would be meaningless (bearing in mind that the test framework has no way to tell if you wrote unit tests for it let alone if they are "correct")
Could you just create a comparer in your test?
var com = new MyComparer<Foo>();
int expected=0;
int actual = com.Compare(a,b);
if (actual!=0)
{
Assert.Fail("oops");
}
Maybe not ideal, but should work...
I also found this question from a few years ago on msdn, with no answers - but an interesting approach to the workaround by the question poster.

Categories