2012-07-05 85 views
2

有誰知道爲什麼UsernameExists不會返回True。我必須把我的語法搞亂。犀牛模擬和方法不返回正確的值

[TestMethod()] 
     public void GenerateUsername_AppendTwoCharacters_ReturnUsernameWithTwoAppendedCharacters() 
     { 
      var usersRepository = MockRepository.GenerateStub<IUsersRepository>(); 
      var target = new StudentsService(null, null, usersRepository, null, null, null, null); 

      usersRepository.Expect(q => q.UsernameExists("", null)).Return(true); 

      var actual = target.GenerateUsername("test", "student", "280000"); 
      Assert.AreEqual("A", actual); 
     } 

public string GenerateUsername(string firstName, string lastName, string studentNumber) 
     { 
      var originalusername = new StudentUsernameGenerator(firstName, lastName, studentNumber).Generate(2, 2, 4); 

      var username = originalusername; 
      if (!string.IsNullOrWhiteSpace(username)) 
      { 
       decimal maxCharacters = 26; 
       var counter = 0; 
       var overflow = 1; 

       while (_usersRepository.UsernameExists(username, null)) 
       { 
        counter++; 

        if (counter > maxCharacters) 
        { 
         overflow++; 
         counter = 1; 
        } 

        username = GetCharacterPaddingForDuplicateUsername(counter, overflow, originalusername); 
       } 
      } 

      return username; 
     } 

回答

3

我不得不添加IgnoreArguments

usersRepository.Stub(q => q.UsernameExists("", null)).IgnoreArguments().Return(true);