首页 \ 问答 \ 将变量连接到HTML标记(Concatenate a variable to an HTML tag)

将变量连接到HTML标记(Concatenate a variable to an HTML tag)

我试图仅在变量不为null时才显示变量旁边的箭头。 有问题的变量是flight.c_tof

render() {
    const items = this.state.flights.map((flight) =>
        <tr key={flight.uid}>
            <td>{flight.c_tof == null ? null : flight.c_tof + <span className="icon-arrow-right icon-small"></span>}</td>
        </tr>
    )

    return ...{items}...
}

这输出类似于: LPPD[object Object]

如果我删除了flight.c_tof + ,那么它会显示该箭头。 我该如何连接该变量?


I am trying to show an arrow next to a variable only if this variable is not null. The variable in question is flight.c_tof.

render() {
    const items = this.state.flights.map((flight) =>
        <tr key={flight.uid}>
            <td>{flight.c_tof == null ? null : flight.c_tof + <span className="icon-arrow-right icon-small"></span>}</td>
        </tr>
    )

    return ...{items}...
}

This outputs something like: LPPD[object Object]

If I remove flight.c_tof +, then it shows that arrow as it should. How can I concatenate that variable?


原文:https://stackoverflow.com/questions/46495911
更新时间:2024-05-04 14:05

最满意答案

使用new实例化对象的代码直接导致测试困难。 通过使用Factory模式,您可以在要测试的代码之外移动对象的创建。 这给你一个注入模拟对象和/或模拟工厂的地方。

请注意,您选择MyTest作为要测试的类是有点不幸的,因为名称暗示它是测试本身。 我会将其更改为MyClass 。 另外,在构造函数中创建对象可以很容易地通过传递A的实例来替换,所以我将把创建移动到稍后调用的方法。

public interface AFactory
{
    public A create(int x, int y);
}

public class MyClass
{
    private final AFactory aFactory;

    public MyClass(AFactory aFactory) {
        this.aFactory = aFactory;
    }

    public void doSomething() {
        A a = aFactory.create(100, 101);
        // do something with the A ...
    }
}

现在你可以在测试中传入一个模拟工厂,它会创建一个你选择的模拟AA 我们使用Mockito创建模拟工厂。

public class MyClassTest
{
    @Test
    public void doSomething() {
        A a = mock(A.class);
        // set expectations for A ...
        AFactory aFactory = mock(AFactory.class);
        when(aFactory.create(100, 101)).thenReturn(a);
        MyClass fixture = new MyClass(aFactory);
        fixture.doSomething();
        // make assertions ...
    }
}

Code that instantiates objects using new directly makes testing difficult. By using the Factory pattern you move the creation of the object outside of the code to be tested. This gives you a place to inject a mock object and/or mock factory.

Note that your choice of MyTest as the class to be tested is a bit unfortunate as the name implies that it is the test itself. I'll change it to MyClass. Also, creating the object in the constructor could easily be replaced by passing in an instance of A so I'll move the creation to a method that would be called later.

public interface AFactory
{
    public A create(int x, int y);
}

public class MyClass
{
    private final AFactory aFactory;

    public MyClass(AFactory aFactory) {
        this.aFactory = aFactory;
    }

    public void doSomething() {
        A a = aFactory.create(100, 101);
        // do something with the A ...
    }
}

Now you can pass in a mock factory in the test that will create a mock A or an A of your choosing. Let's use Mockito to create the mock factory.

public class MyClassTest
{
    @Test
    public void doSomething() {
        A a = mock(A.class);
        // set expectations for A ...
        AFactory aFactory = mock(AFactory.class);
        when(aFactory.create(100, 101)).thenReturn(a);
        MyClass fixture = new MyClass(aFactory);
        fixture.doSomething();
        // make assertions ...
    }
}

相关问答

更多