Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Saturday, December 20, 2014

QR Generation ASp.net MVC

The following C# ASP.net MVC project was done in Visual Studio 2012. The Zxing Dlls were obtained in
http://zxingnet.codeplex.com
After obtaining the dynamically linked libraries (dlls), It is time to create the Asp.net Web project. The figure below shows that the example was made using .Net Framework 4.5.1 while the language is C# using the ASP.net MVC Web project template.
image

Upon creating the project, Go to a the Home View files and open the index.cshtml
image
Feel free to change the text provided by the template before an initial test of the page. You can test by
pressing the button beside the browser.
image

The Page should appear with the expected content
image
Reference the Zxing Dlls
image
Browse to wherever the dlls are located. Then check if the zxing dlls were successfully referenced.
image
image
Add a class for the QR code generator. Name the class
image
The class was named QRCodeGenerator
Below is the code for that class
-----


----
Next step is to add a controller method that will call the QRCodeGenerator class's method returning the rendered image. The code of the method is seen below. In this example the Home Controller was given the method that returns the html code with the QR image of a given text
---

---
Enter the code for the cshtml. In this example the index under the Home contained the code for the Qr
Generation.

---
In my example the following code were modified after Visual Studio generated the Asp.net MVC C#
project
image
Look at the results. The solution should be ready to be built and published
image
After the publishing the page should appear on a browser. Feel free to enter a url on the textbox and
press the Get QR button
image


Saturday, August 23, 2014

Unit Testing C#

Unit test shown below are done with Microsoft unit test framework. Unit test is done to make sure properties, attributes and actions work as expected. A Professional IDE for developers needs to have unit test support since unit testing is a requirement in quality software development. Visual studio being a professional IDE, has capabilities for unit testing including the test explorer Microsoft cited to run unit tests efficiently. Moreover, Visual studio has capabilities in simplifying unit test creation. There are test cases and annotations already so Visual Studio is aware if a class is intended for unit testing or not. Microsoft recommended that test methods are made as soon as the method was created to define and check expectations.
First make sure there is/are class/classes to test on. Afterwards, add a test project in the solution. The test project is where test classes can be created and defined.


Next, make sure the test project has reference of the project that has the classes to be tested. This step ensures that the test classes (or the classes with test methods) can call the classes to be tested.


You can rename the auto generated test class or add your own Test class. This is also a good time to have the test explorer.


There is a class below that has mathematical functions. The other class is a test class that determines if the results from the mathematical functions are correct. Pardon the awful naming convention.
Mathematical function caller
public class MathOperator
    {
        public int Fibonacci(int number)
        {
            if (number <= 1)
                return number;
            return Fibonacci(number - 1) + Fibonacci(number - 2);
        }

        public int Summation(int maxNumber)
        {
            if (maxNumber <= 1)
                return maxNumber;
            return maxNumber + Summation(maxNumber - 1);
        }

    }

Test class
[TestClass]
    public class MathFuncTest
    {
        [TestMethod]
        public void TestFibonacci()
        {
            MathOperator toBeTested = new MathOperator();
            Assert.AreEqual(toBeTested.Fibonacci(2), 1);
        }

        [TestMethod]
        public void TestFibonacci_F3()
        {
            MathOperator toBeTested = new MathOperator();
            Assert.AreEqual(toBeTested.Fibonacci(3), 2);
        }

        [TestMethod]
        public void TestFibonacci_F8()
        {
            MathOperator toBeTested = new MathOperator();
            Assert.AreEqual(toBeTested.Fibonacci(8), 21);
        }

        [TestMethod]
        public void TestSummation_n100()
        {
            MathOperator toBeTested = new MathOperator();
            Assert.AreEqual(toBeTested.Summation(100), 5050);
        }
    }

As seen above, the test class contains an annotation [TestClass] indicating that the class is made for testing purposes and not to be part of the build. The [TestMethod] annotation indicates that the method is to be called when tests are commanded to be run. Running tests can be done from the test explorer where the user can click Run all. Tests can either pass or fail. The results will be visible on the Test explorer. Pass is given when the assertion has the expected result. A fail is given when he assertion did not have the expected result or when an unanticipated exception occurred. Furthermore, a right click on the test class name allows the user to run tests in the class by clicking the Run Tests option or using the keyboard shortcut specified.

All of these are done for the sake of having expectations for classes and their methods. Unit testing is a part of professional software development as evident by the fact that Visual Studio supports it.So be ready in making test classes especially on test driven software projects.