mercredi 29 juin 2016

Mocking with Substitute.ForPartsOf still goes inside that method

In my UserValidation class I am testing a method which that method is internally calling another method. But I just want to return some fake value for that internal method and move on! But the problem is that it is still actually going inside that method.

The method I want to just give me a mock result is this:

public virtual List<Web_Pharmacy_Users> QueryUsersByEmail(string emailAddress)
{
    var query = (from u in _cactusDbContext.Web_Pharmacy_Users
        where
            (u.Email.Trim().ToUpper() == emailAddress.Trim().ToUpper()
             && u.Active)
        select u);

    return query.ToList();
}

where _cactusDbContext is coming through dependency injection and Web_Pharmacy_Users is a DbSet table.

The way I tried to mock its result is like this below:

    ICactusDbContext fakeDbContext = NSubstitute.Substitute.For<ICactusDbContext>();

    var partiallyFaked = Substitute.ForPartsOf<UserValidation>(fakeDbContext);

    List<Web_Pharmacy_Users> webUsers = new List<Web_Pharmacy_Users>();

    try
    {
        partiallyFaked.QueryUsersByEmail("a@abc.com").Returns(webUsers);
    }
    catch (Exception e)
    {
        var dfgf = e.ToString();
        throw;
    }

But it throws a null exception because it tries to actually run that method so goes to this part of code in _cactusDbContext.Web_Pharmacy_Users and it is null.

I thought it shouldn't even try to go inside and run the actual code. What am I doing wrong?

How can you unit test Leaflet JS maps?

How can you unit test Leaflet JS maps?

Mocha.js test with Chai.js not working in asserting numeric values

I am facing a problem in writing a test case using Mocha and Chai. In the following code value of n is 1. I checked it using console.log(). Though I am testing this value against 0 the test is still passing. Actually it doesn't matter against what value n is tested, the test is passing anyway. What is the problem in it? Can anyone help?

it("Should have 1 variables", function(){                                    
    var variable_count = require("../../lib/variable_count").variable_count; 
    var file = __dirname + '/cases/case_3.json';                             
    jsonfile.readFile(file, function(err, obj) {                                                            
        var n = variable_count(obj);                                                                        
        expect(n).to.equal(0);                                                                         
        assert.strictEqual(n, 0);                                                                           
    });                                                                                                     
});

Can proper OO be used with writing HTML helpers with MVC?

Can proper OO be used with writing HTML helpers with MVC? or is there a deeper reason for holding on to usage of static methods and things that can not be unit tested like TextBoxFor etc. ?

Every tutorial/book/video is using archaic procedural programming methods, on a recent project I was told not to use OO modelling and continue with writing everything in static methods and static helper functions!

Is there a real reason for every sample negating all the OO prinicples just because using helper extensions has to be done as a static method and continuing on using Text Writers line by line? Any OO modelled html components can be unit tested, more clear and concise in intention.

To make the matters worse TagBuilder is used as a mediocre text massaging utility, were as modelling with TagElements (if there was such a thing) and calling render on a parent containing child elements would render the entire DOM. Why is writing line by line is preferred instead of constructing objects that each can be programmatically accessed as specific type or IHTMLElement/ITagElement/IWathever interface?

What am I missing? This is the same method that is used to manipulate an html document loaded into a browser, except that in this case the object are being constructed and rendered into a browser. Delphi had HTML DOM object library in 1999. Why HTMLHelper promotes procedural archaic non unit testable programming?

Manipulating return value of inner function for unit testing?

I am currently trying to write the unit test for some legacy code. In one of my function, it calls another function. According to the return value of the inner function, three branching conditions are present. The code is written in C++ and I am using cxxtest framework. How can I manipulate the return value of the inside function without going into the implementation of it so that I can pass through all the branching condition? I prefer to avoid dependencies between the functions. Is there any way to do this?

Unresolved reference: DaggerTestComponent (Kotlin with Dagger for Test)

When we use Dagger and Kotlin, we'll need the following in our build.gradle dependency

kapt 'com.google.dagger:dagger-compiler:2.0'
compile 'com.google.dagger:dagger:2.0'
provided 'org.glassfish:javax.annotation:10.0-b28'

As stated in http://ift.tt/29obx6o

When I try to perform testing using Dagger, and generate the DaggerTestComponent.builder() as per http://ift.tt/293uTPP or even http://ift.tt/29oaZ0m, in Kotlin language, but have the below error

Error:(14, 25) Unresolved reference: DaggerTestComponent

I found http://ift.tt/293uVHv which explain how to get DaggerTestComponent generated, and try put the below in my dependency.

androidTestApt 'com.google.dagger:dagger-compiler:2.0.1'

Apparently, I think this is for Java and not Kotlin, so the issue persist. Is there a Kotlin version of it? How to get my DaggerTestComponent generated in my Kotlin project?

Google Mock: return Vector

I am new to gtest and gmock framework, i am writing unit test case for application where method return vector.

I want to mock function which is returning vector using gmock, what will be possible EXPECT_CALL syntax for returning vector value.

syntax for my function is as below vector GetStringList();

Please suggest possible solution for this.