将变量连接到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
最满意答案
使用
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 ... } }
现在你可以在测试中传入一个模拟工厂,它会创建一个你选择的模拟
A
或A
我们使用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 toMyClass
. Also, creating the object in the constructor could easily be replaced by passing in an instance ofA
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 anA
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 ... } }
相关问答
更多-
当你可以进行基于规范的自动化测试时,单元测试是针对chump的。 您可以使用QuickCheck提供的Arbitrary类型类(您正在寻找的概念是Arbitrary )生成任意(模拟)函数,并使用QuickCheck使用尽可能多的“模拟”函数测试您的函数。 “依赖注入”是一种隐式参数传递的退化形式。 在Haskell中,你可以使用Reader或Free来实现同样的事情,而不用大惊小怪。 Unit testing is for chumps, when you can have Automated Speci ...
-
最简单的方法是创建一个实现接口的内部类,实现返回所需数据的方法,然后在测试用例中使用它。 例如: public void testMethod ( ) { MyInterface mockObject = new MyInterface ( ) { public void myMethod ( ) { // NOOP } public int myFunction ( ) { ...
-
根据我的经验,你的代码的水平较低(不太平凡),相对于编写它们所需的努力,单位测试的价值越高。 随着食物链的上升,测试变得越来越复杂,越来越昂贵。 单元测试是至关重要的,因为它们在重构过程中断开某些操作时会告诉您。 较高级别的测试有自己的价值,但是不再称为单元测试; 它们被称为集成测试和验收测试。 需要集成测试,因为它们告诉您不同软件组件在一起工作的情况。 验收测试是客户签收的。 验收测试通常由其他人(而不是程序员)编写,以提供不同的观点; 程序员倾向于编写测试工作,测试人员试图通过测试不起作用来破解它。 模 ...
-
使用moq,使用.Setup方法: m_objectMock.Setup(m => m.Id).Returns("myMockId"); 这意味着当消费代码调用模拟Id的值时,模拟对象将返回myMockId 。 请参阅快速入门上的属性部分。 With moq, use the .Setup method: m_objectMock.Setup(m => m.Id).Returns("myMockId"); This means when the consuming code calls the mock ...
-
由于Task
是Task 的子类型,您可以使用Task.FromResult,它应该可以正常工作: Task fakeTask = Task.FromResult -
你有一个错字。 这是shouldReceive而不是shouldRecieve You have a typo. It is shouldReceive instead of shouldRecieve
-
我猜你的模块名称也是Customer ,当你在import Customer ,它会导入模块而不是类。 代之以做 from Customer import Customer 在测试模块中。 I guess your module name is also Customer and when you are doing import Customer , it is importing the module and not the class. Do this instead from Customer ...
-
使用new实例化对象的代码直接导致测试困难。 通过使用Factory模式,您可以在要测试的代码之外移动对象的创建。 这给你一个注入模拟对象和/或模拟工厂的地方。 请注意,您选择MyTest作为要测试的类是有点不幸的,因为名称暗示它是测试本身。 我会将其更改为MyClass 。 另外,在构造函数中创建对象可以很容易地通过传递A的实例来替换,所以我将把创建移动到稍后调用的方法。 public interface AFactory { public A create(int x, int y); } p ...
-
是的,您可以,只要您确实使用CallBase调用具体的类,并使用As<>来定位适当的接口/基类: var mockClass = new Mock
().As (); mockClass.CallBase = true; mockClass.SetupGet(m => m.Property1).Returns("Mock"); Assert.AreEqual("Mock", mockClass.Object.Property1); Assert.AreEqual( ... -
如何对测试对象内创建的模拟对象产生期望?(How to make expectations on a mock object created inside the tested object?)[2022-02-19]
如果您不想修改接口或引入额外的间接,则可以使用Typemock Isolator ++。 templateclass MyClass { public: void someMethod() { T object; object.doSomething(); } }; class RealType //RealType is the actual production type, no injection need ...