我已经阅读了FluenAssertions Extensibility页,但是如果我已经为对象实现了class AnotherViewController: UIViewController {
lazy var scrollView: UIScrollView = {
let view = UIScrollView()
view.translatesAutoresizingMaskIntoConstraints = false
// don't do this... let auto-layout and constraints handle the scrollable area
// view.contentSize.height = 1200
//so we can see it
view.backgroundColor = .yellow
return view
}()
let stepOne: UILabel = {
let label = UILabel()
let attributedTitle = NSMutableAttributedString(string: "Step 1:\n", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 45)])
attributedTitle.append(NSAttributedString(string: "This is Step 1", attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20)]))
label.attributedText = attributedTitle
label.numberOfLines = 2
return label
}()
let stepOneDetails: UILabel = {
let label = UILabel()
let attributedTitle = NSMutableAttributedString(string: "Step 1 Details:\n", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 45)])
attributedTitle.append(NSAttributedString(string: "This is Step 1 Details", attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20)]))
label.attributedText = attributedTitle
label.numberOfLines = 2
return label
}()
let stepTwo: UILabel = {
let label = UILabel()
let attributedTitle = NSMutableAttributedString(string: "Step 2:\n", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 45)])
attributedTitle.append(NSAttributedString(string: "See Your Doctor a 2nd time", attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20)]))
label.attributedText = attributedTitle
label.numberOfLines = 2
return label
}()
let stepTwoDetails: UILabel = {
let label = UILabel()
let attributedTitle = NSMutableAttributedString(string: "Step 2 Details:\n", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 45)])
attributedTitle.append(NSAttributedString(string: "This is Step 2 Details", attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20)]))
label.attributedText = attributedTitle
label.numberOfLines = 2
return label
}()
let reportButton: UIButton = {
let b = UIButton()
b.setTitle("Report Button", for: .normal)
b.backgroundColor = .blue
return b
}()
override func viewDidLoad(){
super.viewDidLoad()
view.addSubview(scrollView)
setupScrollView()
setupScrollViewLabels()
}
func setupScrollView(){
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 200.0),
scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0.0),
scrollView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 0.0),
scrollView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: 0.0),
])
}
func setupScrollViewLabels(){
scrollView.addSubview(stepOne)
scrollView.addSubview(stepOneDetails)
scrollView.addSubview(reportButton)
scrollView.addSubview(stepTwo)
scrollView.addSubview(stepTwoDetails)
// we're using auto-layout
reportButton.translatesAutoresizingMaskIntoConstraints = false
// for each of the labels
[stepOne, stepOneDetails, stepTwo, stepTwoDetails].forEach {
// we're using auto-layout
$0.translatesAutoresizingMaskIntoConstraints = false
// set background colors so we can see the frames of the elements
$0.backgroundColor = .cyan
}
NSLayoutConstraint.activate([
// stepOne to top + 10 padding and leading/trailing + 20 padding
stepOne.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 10.0),
stepOne.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 20.0),
stepOne.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor, constant: 20.0),
// stepOneDetails to stepOne + 40 padding and leading/trailing + 20 padding
stepOneDetails.topAnchor.constraint(equalTo: stepOne.bottomAnchor, constant: 40.0),
stepOneDetails.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 20.0),
stepOneDetails.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor, constant: 20.0),
// reportButton to stepOneDetails + 60 padding and leading/trailing + 20 padding
reportButton.topAnchor.constraint(equalTo: stepOneDetails.bottomAnchor, constant: 60.0),
reportButton.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 20.0),
reportButton.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor, constant: 20.0),
// reportButton height to 50
reportButton.heightAnchor.constraint(equalToConstant: 50.0),
// stepTwo to reportButton + 50 padding and leading/trailing + 20 padding
stepTwo.topAnchor.constraint(equalTo: reportButton.bottomAnchor, constant: 50.0),
stepTwo.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 20.0),
stepTwo.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor, constant: 20.0),
// stepTwoDetails to stepTwo + 40 padding and leading/trailing + 20 padding
stepTwoDetails.topAnchor.constraint(equalTo: stepTwo.bottomAnchor, constant: 40.0),
stepTwoDetails.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 20.0),
stepTwoDetails.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor, constant: 20.0),
// also must define a width-basis for the scrollable area (the .contentSize),
// so use stepOne label, and make it the width of the scrollView minus 40 (20-pts on each side)
stepOne.widthAnchor.constraint(equalTo: scrollView.widthAnchor, constant: -40.0),
// and we have to constrain the last element to the bottom of the scrollView
// with 10 padding (note: it must be negative)
stepTwoDetails.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: -10.0),
])
}
}
,同时又保留了一个对象,我仍然无法理解如何创建一个Should().BeEquivalentTo()
比较方法来收集对象同一集合的所有其他断言方法。
这是一个简化的示例: 我要检查的课程:
Should().BeEquivalentTo()
我在Specflow测试套件中转换表行的类
public class CalculationResult
{
public DateTime Date { get; set; }
public int Id { get; set; }
public bool CalcState { get; set; }
public double Result { get; set; }
}
类间比较类为:
public class CalcResultBdd
{
public DateTime Date { get; set; }
public double Result { get; set; }
}
链接FluentAssertions和自定义断言类的静态类:
public class CalculationResultAssertions : ReferenceTypeAssertions<CalculationResult, CalculationResultAssertions>
{
public CalculationResultAssertions(CalculationResult instance)
{
Subject = instance;
}
protected override string Identifier => "CalculationResult";
[CustomAssertion]
public AndConstraint<CalculationResultAssertions> BeEquivalentTo(
CalcResultBdd expectedResult,
double precision = 0.001,
string because = "",
params object[] becauseArgs)
{
Execute.Assertion.
BecauseOf(because, becauseArgs).
ForCondition(expectedResult != null).
FailWith("You cannot assert that calculation result is correct if you do not pass a proper object").
Then.
ForCondition(precision >= 0).
FailWith("You cannot compare double values if you provide negative precision").
Then.
Given(() => Subject).
ForCondition(s => s.CalcState).
FailWith("Expected CalcState to be \"true\"").
Then.
ForCondition(s => s.Date == expectedResult.Date).
FailWith(
"Expected Date to be {0}{reason}, but found {1}",
_ => expectedResult.Date.ToString("yyyy-MM-dd"),
s => s.Date.ToString("yyyy-MM-dd")).
Then.
ForCondition(s => Math.Abs(s.Result - expectedResult.Result) <= precision).
FailWith(
"Expected Result to be {0} with precision {1}{reason}, but found {2}",
_ => expectedResult.Result,
_ => precision,
s => s.Result);
return new AndConstraint<CalculationResultAssertions>(this);
}
}
如果我运行以下命令,而没有为public static class AssertionExtensions
{
public static CalculationResultAssertions Should(this CalculationResult instance)
{
return new CalculationResultAssertions(instance);
}
}
重新定义BeEquivalentTo
List<CalculationResult>
FluentAssertions将使用内置结构比较而不是自定义比较。我是否可以更改代码中的某些内容,以使Fluent断言改为使用自定义比较?
答案 0 :(得分:0)
如果我没记错的话,BeEquivalentTo
扩展方法期望是通用的,并且也将取代您的扩展方法。