ReactiveUI 不响应测试中的更改(使用 Autofixture)
ReactiveUI not responding to changes in test (with Autofixture)
看下面的代码(简单的)
public class NewEventViewModel : ReactiveObject
{
readonly ObservableAsPropertyHelper<bool> isSendVisible;
public bool IsSendVisible
{
get { return isSendVisible.Value; }
}
private ReactiveList<string> users;
public ReactiveList<string> Users
{
get { return this.users; }
set { this.RaiseAndSetIfChanged(ref users, value); }
}
public NewEventViewModel()
{
Users = new ReactiveList<string>(new List<string>())
{
ChangeTrackingEnabled = true
};
Users.CountChanged
.Select(x => x > 0)
.ToProperty(this, x => x.IsSendVisible, out isSendVisible);
//Users.Add("commented");
}
}
测试:
[Fact]
public void ShouldBeVisibleWhenIsAtLeastOneUser()
{
//var sut = new NewEventViewModel();
var fixture = new Fixture();
var sut = fixture.Create<NewEventViewModel>();
sut.Users.Add("something");
sut.IsSendVisible.ShouldBeTrue();
}
测试失败...但是当我取消注释 ViewModel 中的行时,它通过了。
看起来测试忽略了对用户的更改。它在我手动创建 sut 时起作用(new NewEventViewModel())。为什么 AutoFixture 以如此奇怪的方式打破测试?我做错了什么?
您可以使用
fixture.Build<NewEventViewModel>().OmitAutoProperties().Create()
暂时关闭自动属性。
看下面的代码(简单的)
public class NewEventViewModel : ReactiveObject
{
readonly ObservableAsPropertyHelper<bool> isSendVisible;
public bool IsSendVisible
{
get { return isSendVisible.Value; }
}
private ReactiveList<string> users;
public ReactiveList<string> Users
{
get { return this.users; }
set { this.RaiseAndSetIfChanged(ref users, value); }
}
public NewEventViewModel()
{
Users = new ReactiveList<string>(new List<string>())
{
ChangeTrackingEnabled = true
};
Users.CountChanged
.Select(x => x > 0)
.ToProperty(this, x => x.IsSendVisible, out isSendVisible);
//Users.Add("commented");
}
}
测试:
[Fact]
public void ShouldBeVisibleWhenIsAtLeastOneUser()
{
//var sut = new NewEventViewModel();
var fixture = new Fixture();
var sut = fixture.Create<NewEventViewModel>();
sut.Users.Add("something");
sut.IsSendVisible.ShouldBeTrue();
}
测试失败...但是当我取消注释 ViewModel 中的行时,它通过了。
看起来测试忽略了对用户的更改。它在我手动创建 sut 时起作用(new NewEventViewModel())。为什么 AutoFixture 以如此奇怪的方式打破测试?我做错了什么?
您可以使用
fixture.Build<NewEventViewModel>().OmitAutoProperties().Create()
暂时关闭自动属性。