Có vẻ tốt! Tuy nhiên, bạn cũng có thể sử dụng nó declaratively với xUnit.net extension.
Giả sử rằng các loại được sử dụng trong các thử nghiệm được định nghĩa là:
public class CardHolderCustomer
{
}
public interface ICustomerAdapter
{
CardHolderCustomer BuildCustomer();
}
public class CardHolderViewModel
{
private readonly ICustomerAdapter adapter;
public CardHolderViewModel(ICustomerAdapter adapter)
{
if (adapter == null)
throw new ArgumentNullException("adapter");
this.adapter = adapter;
}
public CardHolderCustomer Customer
{
get
{
return this.adapter.BuildCustomer();
}
}
}
Các thử nghiệm ban đầu có thể được viết như:
[Theory, DomainTestConventions]
public void CustomerPropertyIsCorrect2(
CardHolderCustomer expected,
[Frozen]Mock<ICustomerAdapter> builderStub,
CardHolderViewModel sut)
{
builderStub
.Setup(x => x.BuildCustomer())
.Returns(expected);
var actual = sut.Customer;
Assert.Equal(expected, actual);
}
Các DomainTestConventionsAttribute
được định nghĩa là:
internal class DomainTestConventionsAttribute : AutoDataAttribute
{
internal DomainTestConventionsAttribute()
:base(new Fixture().Customize(new DomainTestConventions()))
{
}
}
Các DomainTestConventions
được định nghĩa là:
internal class DomainTestConventions : CompositeCustomization
{
internal DomainTestConventions()
:base(new AutoMoqCustomization())
{
}
}
Lưu ý rằng DomainTestConventions
xuất phát từ CompositeCustomization
mà về cơ bản có nghĩa là bạn có thể tạo thêm tuỳ chỉnh và thêm chúng như tham số cho các nhà xây dựng cơ bản.
Bạn cũng có thể đọc:
Hy vọng rằng sẽ giúp.
Nguồn
2013-03-05 05:23:19
Cảm ơn bạn! Đó chính là phản hồi tôi đang tìm kiếm. – cocogorilla
Tôi chỉ cần thêm ... thực tế bạn sao chép mã mù của tôi chứng minh tốt hơn bất kỳ cuốn sách nhanh nhẹn nào có thể kiểm tra là nguồn tài liệu chính cho mã. – cocogorilla
+1 Chỉ là một câu trả lời tuyệt vời –