2015-09-17 105 views
0

我按照說明How to mock an import從進口模擬進口

但我沒有成功。

我有以下設置:

Class A進口Class B,並Class B進口Class C

Class C是我想模擬的模塊。

test.py

import sys 
from mock import Mock 
sys.modules['C'] = Mock() 
import A 
print A.C.__name__ // this returns: 

AttributeError: 'module' object has no attribute 'C' 

回答

2

應該A.B.C.__name__這樣的:

import sys 
from mock import Mock 
sys.modules['C'] = Mock() 
import A 
print A.B.C.__name__