Posty

Wyświetlam posty z etykietą c#

AutoCodeCoverage - how it can help you find a bug

Obraz
Hi Last time I shown you what is AutoCodeCoverage. Today I would like to show you how it can help you. I'll show you how it can find small mistakes in you code. Let's start. Last time i shown my example solution which was covered in 100% by unit tests with AutoCodeCoverage.  There were 3 projects - SimpleClassLibrary, SimpleClassLibrary2 and UnitTestProject1. It looked like:  Now it looks like: I added more empty implementations of interface Inter1Lib2 and change logic of Class2. Old Class2 was looking like this: namespace SimpleClassLibrary { public class Class2 { public void HowIWasCreated(Class1 cl) { if (cl == null) { Console.WriteLine("null"); return; } if (cl.HowCreated() == EnumHowCreated.DefaultConstructor) { Console.WriteLine("Default"); } if (cl.HowCreated() == EnumHowCreated.ParameterConstructor) { Console.WriteLine("Paramet

AutoCodeCoverage - nuget package for covering .net code with tests

Obraz
Hi I was wondering for some time about code coverage in c#. I'm using a lot of DTO objects which should be covered by unit tests. I thought that it should be covered by default. I wrote simple project FakeCodeCoverage. It used reflection to cover the simplest way in constructors and methods. It was not enough for me. It was really only fake coverage. I abandoned this project but I started new project AutoCodeCoverage. This is different from the previous one because it tries cover all statements, branches and conditions. As result you can get all exceptions which was thrown while invoking methods, constructors, properties, etc. It allows to inject implementation of interface or instance of type which will be used at all times when this type will be exist as parameter. In DTOs or simple projects AutoCodeCoverage could cover up to 100% lines of your code. It is published on nuget https://www.nuget.org/packages/AutoCodeCoverage Actually it support .net4.x, .netcore

[Part 1] Is c# safe? Are Internal class accessible only from the same assembly?

Obraz
Hi I was wondering for some time about encapsulation in C#. Are internal class accessible only from the same assembly? Are private fields accessible only from inside class? Quoting the Microsoft website: "Private access is the least permissive access level. Private members are accessible only within the body of the class or the struct in which they are declared...." "Internal types or members are accessible only within files in the same assembly..." It looks like true. I believed it, but I decided to check it out. There is a possibility to read and modify private fields and create instances of internal classes when reflection is used. Reflection is the built-in mechanizm allowing to dynamically create instance of class or invoke methods. Unfortunately it allows access to private fields and much more... I wrote a simple example showing how powerful is reflection. Code is uploaded to github.  GITHUB - PrivateFieldsInternalClass My soluti