CategoryUncategorized

Parameterizing Unit Tests using xUnit

xUnit allows our unit test to use parameters, which allows us to test our code for multiple scenarios without needing to write multiple methods with same content.

Let’s look at example from previous lesson

Here we test if 3 + 9 equals 12.

But what if we wanted to test our Add method for multiple scenarios?

 

We could use InlineData Attribute.

InlineData Attribute allows us to run our test with input parameters, and passes those parameters to test method using params object[] data.

We are not done yet.

Compiler is not happy and we are getting error : Fact methods cannot have parameters.

Simple solution is to replace Fact Attribute with Theory Attribute

Now, when we run our unit test we can see that same test method ran 3 times an all test have passedĀ  šŸ™‚

Also note that we can also see input parameters!

Binary serialization

Today’s topic is Binary serialization, if you wish to read more about serialization or other types of serialization please visit this link:

Binary serialization is type of serialization where type fidelity and reference integrity are preserved, meaning assembly information, private and public fields are being preserved as well as theirs memory locations. You would normally use binary serialization to preserve application state, store it to a backing store (storage medium) and use it later to run application on same computer or maybe multiple other computers.

Hearth of binary serialization is BinaryFormatter class and its Serialize and Deserialize methods. Serialize and DeserializeĀ  methods take two parameters stream and object graph and serialize or deserialize object accordingly. Result of serialization are unreadable to human eye but you can read some information:

Binary Formatter

BinaryFormatter serialization result

As you can see type and field information are preserved.

If you wish serialized data in more readable way you can use SoapFormatter. Note that SoapFormatter is obsolete so use it only for debugging and testing purposes.

SoapFormatter

SoapFormatter serialization result

To mark a type serializable use Serializable attribute to decorate type. Serializable attribute is not inherited so if you wish to serialize both base and derived class decorate both of them with serializable attribute. Other approach would be to implement ISerializable interface.

If you wish not to serialize filed add NonSerialized attribute.

You can serialize null types, but you can not serialize or deserialize null streams.

Properties are not serialized but their private fields are. Do not serialize auto implemented properties because you cannot guarantee that theirs private fields will have the same name on the other machines as they do on yours.

If we wish more control during Serialization/Deserialization processes we can use attributes from System.Runtime.Serialization and apply them to the methods.
Attributes we can use for methods during serialization:
OnSerializingAttribute – use this attribute to prepare data before deserializing . For example encrypt password field or change distance from miles to kilometers.
OnSerializedAttribute – restores type to a status it was before applying OnSerializingAttribute.

Attributes we can use for methods during serialization:
OnDeserializingAttribute – because serialization cannot use constructor, use this attribute for method that will simulate constructor, and maybe set fields to default values.
OnDeserializedAttribute – use this attribute to manually set value of the fields just after serialization process has finished.

 

Example for Binary serialization.

We will try to serialize data from Employee class:

 

 

Download link for entire solution is here.

Strongly named assemblies

Purpose of strong naming assemblies is to create unique identity for desired assembly. You would typically want to do that when you need multiple versions of same assembly to be used, when assembly must be installed inside GAC (global assembly cache) or when you want assemblies to be to be referenced by strongly named assemblies.

What does it mean to be strongly named?

Strongly named assemblies are assemblies that have:

Name (usually name of project)

AssemblyVersion (AssemblyVersion information)

Culture information (Assemblies are usually culture neutral but if you wish to change them you can access AssemblyInfo.cs file and change AssemblyCulture attribute)

Private/Public key token (main difference between strongly named assemblies and weakly named assemblies)

I wish to create strongly named assemblies! What do i do?

First you would create a project. That first step would create weakly named assembly. Next what you want to do is sign it. There are multiple ways to achieve this.

First Approach:

From Visual studio:

  1. Right click project
  2. Properties
  3. Select Signing tab
  4. Check “Sign the assembly” CheckBox
  5. From Drop-down control select New
  6. Input necessary Information
  7. Select Drop-down control again and find yourĀ  .pfx file

Strongly named assemblies

How to strongly name assemblies

Now when we know how to strong name assemblies we can continue to part on how to put an assembly in the global assembly cache.

 

 

© 2019 LearnC#WithDarko

Theme by Anders NorenUp ↑