Friday 19 April 2013

An Example of Object Oriented in JavaScript

Below is an example of implementing object oriented in JavaScript:
var AutoRefresh = {
  counter : 0,
  timesDuration : 5,
  timerId : null,
  autoRefreshIsRunning : false,
  runProcess : function() {
        AutoRefresh.counter++;
        AutoRefresh.autoRefreshIsRunning = true;
        . . .
  },
  stopProcess : function() {      
        AutoRefresh.counter = 0;
        AutoRefresh.autoRefreshIsRunning = false;
        . . .
  }
};

Friday 5 April 2013

Moq - Checking the Order of Methods Called

Say we have codes like below that call two methods:
public void InsertOrUpdate(ItemDto itemDto)
{
    . . .
 context.Add<Item>(item);
 context.SaveChanges();
    . . .
}

We would like to ensure that a method is called after the other. We can use the Callback feature in Moq and a collection type to store predefined values for each method. Then we can check the values stored in the collection and their orders to determine that the methods were called in sequence.
[Fact]
public void SaveChanges_method_is_called_after_Add_method()
{
    // Arrange
    List<string> invokes = new List<string>();
    string addMethod = "Add";
    string saveChangesMethod = "SaveChanges";
    context.Setup(c => c.Add<Item>(It.IsAny<Item>()))
           .Callback(() => invokes.Add(addMethod));
    context.Setup(c => c.SaveChanges())
           .Callback(() => invokes.Add(saveChangesMethod));
            
    // Act
    itemDS.InsertOrUpdate(itemDto);
               
    // Assert
    Assert.Equal(invokes[0], addMethod);
    Assert.Equal(invokes[1], saveChangesMethod);
}