2011-08-04 37 views
0

我在ruby中編寫了一個帶有1個函數的簡單類。這用於從字符串中刪除空格。Ruby名稱錯誤問題

我的代碼:

module TestString 
     class StringUtils  
     # 
     # Delete space from string 
     # 
     def remove_space str 
      space = " " 
      str.delete space 
     end 
     end 
     end 

現在我試着寫了這個功能簡單的測試:

require 'teststring' 

class TestStringUtils < Test::Unit::TestCase 
    def test_remove_space 
    assert_equal("Teststring", TestString::StringUtils.new().remove_space("Test string")) 
    end 
end 

當我嘗試運行測試,我得到錯誤:

1) Error: 
test_remove_space(TestStringUtils): 
NameError: uninitialized constant TestStringUtils::Json 
    /home/workspace/lib/test.rb:16:in `test_remove_space' 

爲什麼?請解釋我有什麼問題?

謝謝。

回答

1

您需要將require文件與您的TestStringUtils一起存入測試文件中。

+0

另外,除非代碼示例不完整,否則StringUtils不在名爲Json的模塊內。 – sepp2k

+0

謝謝你的回覆,我在test.rb中有TestStringUtils代碼,我必須在test.rb中放置require'test',我理解正確嗎? – 0xAX

+0

是的,你說得對,謝謝你,我混合了TestString模塊中的類StringUtils。 – 0xAX