是否可以使用FakeItEasy创建一个受测试的类,其中构造函数中声明的所有依赖项都是使用伪造自动初始化的?
想象一下这个课程:
public class Inserting
{
public Inserting(
ITransactionService transactionService,
ISharedData sharedData)
{
TransactionService = transactionService;
SharedData = sharedData;
}
public ITransactionService TransactionService { get; }
public ISharedData SharedData { get; }
public void Enter()
{
TransactionService.StartTransaction();
}
}
然后我在测试设置中创建所有假对象,并使用这些假装构建我的测试类:
public class InsertingTest
{
private Inserting _inserting;
private ISharedData _fakeSharedData;
private ITransactionService _fakeTransactionService;
[SetUp]
public void SetUp()
{
_fakeTransactionService = A.Fake<ITransactionService>();
_fakeSharedData = A.Fake<ISharedData>();
_inserting = new Inserting(_fakeTransactionService, _fakeSharedData);
}
[Test]
public void TestEnter()
{
// Arrange
// Act
_inserting.Enter();
// Assert
A.CallTo(() => _fakeTransactionService.StartTransaction().MustHaveHappened();
}
}
但我在Java世界中看到,当使用Mockito和Dagger 2时,你可以这样做:
public class PhoneDialer {
private Activity activity;
private PhoneCallListener phoneCallListener;
@Inject
public PhoneDialer(Activity activity, PhoneCallListener phoneCallListener) {
this.activity = activity;
this.phoneCallListener = phoneCallListener;
}
}
public class PhoneDialerTest {
@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();
@Mock
PhoneCallListener phoneCallListener;
@Mock
Activity activity;
@InjectMocks
PhoneDialer dialer;
@Test
public void test_dialer() throws Exception {
// Arrange
// Act
dialer.callNumber("abc");
// Assert
Mockito.verify(phoneCallListener, times(1)).startCall();
}
}
并且使用伪造自动初始化模拟的类。 C#中是否有与FakeItEasy相同的程序或功能?
答案 0 :(得分:3)
我想你想要的东西
Automatically inject fakes in test fixture with FakeItEasy。您可以使用[Fake]
标记要注入的假货,并使用[UnderTest]
标记要测试的生产类型。
我们真的应该把它放到文档中。
可替换地,