2012-03-02 146 views
2

我正在使用Rhino模擬模擬嘲笑我的測試方法。有人能看到測試方法中的TODO部分,並幫助我嘲笑它嗎?模擬犀牛模擬-MVVM

這是我的服務接口:

public interface ICustomersServiceAgent 
{ 
    void GetCustomers(EventHandler<GetCustomersCompletedEventArgs> callback); 
    void SaveCustomer(POC.Model.Customer cust, EventHandler<SaveCustomerCompletedEventArgs> callback); 
} 

這是我的ViewModel

public class CustomerVM : ViewModelBase 
{ 
    private Model.Customer _curentCustomer; 
    private RelayCommand _saveCommand; 
    private ICustomersServiceAgent ServiceAgent { get; set; } 
    private bool isSaved; 
    private RelayCommand _calculateAgeCommand; 
    private Decimal age; 
    private DateTime dateOfBirth; 

    public CustomerVM(ICustomersServiceAgent serviceAgent) 
    { 
     if (serviceAgent == null) 
     { 
      ServiceAgent = ServiceManager.GetCustomerServiceManagement(); 
     } 
     else 
     { 
      ServiceAgent =serviceAgent; 
     } 
     WireCommands(); 
    } 

    // if curent object is null then it should be intialize 
    public Model.Customer CurentCustomer 
    { 
     get { return _curentCustomer ?? (_curentCustomer = new Model.Customer()); } 
     set 
     { 
      if (_curentCustomer != value) 
      { 
       _curentCustomer = value; 
       OnPropertyChanged("CurentCustomer"); 
      } 
     } 
    } 

    public RelayCommand CalculateAgeCommand 
    { 
     get; 
     private set; 
    } 

    private void WireCommands() 
    { 
     SaveCustomerCommand = new RelayCommand(SaveCustomer); 
     SaveCustomerCommand.IsEnabled = true; 
     CalculateAgeCommand = new RelayCommand(CalculateAge); 
    } 

    private void SaveCustomer() 
    { 
     var cus = CurentCustomer; 
     ServiceAgent.SaveCustomer(cus, (s, e) => 
     { 
      IsSaved = e.Result; 
     }); 
    } 

    private void CalculateAge() 
    { 
     Age = DateTime.Now.Year - DateOfBirth.Year;   
    } 

    public RelayCommand SaveCustomerCommand 
    { 
     get; 
     private set; 
    } 

    public bool IsSaved 
    { 
     get { return isSaved; } 
     set 
     { 
      isSaved = value; 
      OnPropertyChanged("IsSaved"); 
     } 
    } 

    public decimal Age 
    { 
     get { return age; } 
     set { 
      age = value; 
      OnPropertyChanged("Age"); 
     } 
    } 

    public DateTime DateOfBirth 
    { 
     get { return dateOfBirth; } 
     set { 
      dateOfBirth = value; 
      OnPropertyChanged("DateOfBirth"); 
     } 
    } 
} 

我想測試在視圖模型的SaveCustomerCommand以上。 所以在我的測試方法中,我想模擬ICustomersServiceAgent界面中的void SaveCustomer(POC.Model.Customer cust, EventHandler<SaveCustomerCompletedEventArgs> callback)方法。

這是我的測試方法,請參閱待辦事項部分

[TestMethod] 
public void SaveCustomerCommandTest() 
{ 
    var customerServiceMock = MockRepository.GenerateMock<ICustomersServiceAgent>(); 
    var customerVM = new POC.SilverlightClient.ViewModel.CustomerVM(customerServiceMock); 

    // TO do : Code to mock SaveCustomer method /////////////////////////////////// 

    var saveCustomerCommand = customerVM.SaveCustomerCommand; 
    saveCustomerCommand.Execute(null); 

    Assert.IsTrue(customerVM.IsSaved); 
} 

可能有人請解釋一下我該怎麼辦呢?

回答

0

我不明白你爲什麼需要模擬SaveCustomer。所有私人的SaveCustomer方法都會調用您已經在嘲笑的ServiceAgent服務。我假設RelayCommand類在調用SaveCustomer的構造函數參數中調用您要發送的委託。你是否試過在你的問題中按原樣運行單元測試?

+0

Yes.But這是行不通的,因爲SaveCustomer方法不嘲笑。它只是模擬ICustomersServiceAgent.So,我想模擬 SaveCustomer方法。很遺憾,我做不到。 – DKD 2012-03-07 12:09:44

+0

如果編輯你的問題來描述單元測試「不起作用」,那麼人們會得到更多關於你的信息正在努力並可能有所幫助。 – 2012-03-07 13:16:18

0

嘗試以下操作:

customerServiceMock.Stub(sa => sa.SaveCustomer(Arg<POC.Model.Customer>.Is.Anything, Arg<EventHandler<SaveCustomerCompletedEventArgs>>.Is.Anything)).WhenCalled(invocation => 
     { 
      ((EventHandler<SaveCustomerCompletedEventArgs>)invocation.Arguments[1])(
customerServiceMock, 
new SaveCustomerCompletedEventArgs { Result = true }); 
     }); 
+0

哦。這是一歲:) – 2013-04-12 09:47:27