๋ค์ ํ ์คํธ๊ฐ ์คํจํฉ๋๋ค.
public class UnitTest1
{
[Fact]
public void Test1()
{
var fixture = new Fixture();
fixture.Customize<PaymentReturnPayload>(c =>
{ // a statement lambda
c.With(a => a.OperationAmount, "1");
c.FromFactory(() => new PaymentReturnPayload(fixture.Create<int>().ToString()));
return c;
});
var paymentReturnPayload = fixture.Create<PaymentReturnPayload>();
Assert.True(int.TryParse(paymentReturnPayload.Control, out _));
}
}
public class PaymentReturnPayload
{
public PaymentReturnPayload(string control)
{
Control = control;
}
public string OperationAmount { get; set; }
public string Control
{
get;
}
}
Control ๋งค๊ฐ๋ณ์๋ ์์ฑ๋ int ๋์ ์์์ ๋ฌธ์์ด๋ก ์ฑ์์ง๋๋ค.
๋ฒ์ 4.0 - 4.4(์ด์ ๋ฒ์ ์ ํ
์คํธํ์ง ์์)
๋คํธ ์ฝ์ด 2.0 - 2.1
์ด๊ฒ์ ์ ์๋ํฉ๋๋ค
[Fact]
public void Test1()
{
var fixture = new Fixture();
fixture.Customize<PaymentReturnPayload>(c =>
c.FromFactory(() => new PaymentReturnPayload(fixture.Create<int>().ToString())));
var paymentReturnPayload = fixture.Create<PaymentReturnPayload>();
Assert.True(int.TryParse(paymentReturnPayload.Control, out _));
}
๋๋ค๊ฐ ์๋ํ์ง ์๋ ๋ฌธ์ ์ฌ์ฉํ์ฌ ์ฌ์ฉ์ ์ง์ ํ๋ ๊ฒ์ฒ๋ผ ๋ณด์
๋๋ค.
์์๋ ํ๋์
๋๊น?
๊ทธ๋ ๋ค๋ฉด ์ผ๋ถ ๋ฌธ์์ ์ถ๊ฐํด์ผ ํ์ง ์์ต๋๊น? ๋ญ๊ฐ ์๋ชป๋๋์ง ์์๋ด๋๋ผ ์๊ฐ์ ๋ง์ด ํ๋นํ๋ค...
@progala2 ์ง๋ฌธ์ ์ฌ๋ ค์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค.
๊ธฐ์กด ๋์์ ์๋๋ ๊ฒ์
๋๋ค. ๊ทธ ์ด์ ๋ FromFactory
, With
(๋ฐ Without
) ๋ฉ์๋๊ฐ ๋ณ๊ฒฝ ๋ถ๊ฐ๋ฅํ๋ฏ๋ก ๊ธฐ๋ณธ ๊ฐ์ฒด๋ฅผ ๋ณ๊ฒฝํ์ง ์๊ธฐ ๋๋ฌธ์
๋๋ค. ๋์ ๊ตฌ์ฑ๋ ๊ทธ๋ํ์ ํจ๊ป ์ ๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค.
์ฒซ ๋ฒ์งธ ํ ์คํธ๊ฐ ์๋ํ์ง ์๋ ์ด์ ๋ ํธ์ถ ๊ฒฐ๊ณผ๋ฅผ ์ฌ์ฉํ์ง ์์ ์ ์ฉ๋ ๋ชจ๋ ๋ณ๊ฒฝ ์ฌํญ์ด ์ญ์ ๋๊ธฐ ๋๋ฌธ์ ๋๋ค. ์ฝ๋๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ๋ค์ ์์ฑํ๋ฉด ๋ช ๋ น๋ฌธ์์๋ ์ ์๋ํฉ๋๋ค.
```c#
[์ฌ์ค]
๊ณต๊ฐ ๋ฌดํจ Test1()
{
var ๊ณ ์ ์ฅ์น = new Fixture();
์ ์ฐฉ๋ฌผ.์ฌ์ฉ์ ์ ์
{
// ๋ฌธ ๋๋ค
var res = c.FromFactory(() => ์๋ก์ด PaymentReturnPayload(fixture.Create)
res = res.With(a => a.OperationAmount, "1");
๋ฐํ ํด์๋;
});
var paymentReturnPayload = fixture.Create<PaymentReturnPayload>();
Assert.True(int.TryParse(paymentReturnPayload.Control, out _));
}
But as you might notice, it's easier to write this code with lambda.
P.S. By the way, you can slightly improve the final code (notice the `FromFactory` has overload taking extra values necessary for the object construction):
```c#
[Fact]
public void Test3()
{
var fixture = new Fixture();
fixture.Customize<PaymentReturnPayload>(c =>
c.FromFactory((int control) => new PaymentReturnPayload(control.ToString())));
var paymentReturnPayload = fixture.Create<PaymentReturnPayload>();
Assert.True(int.TryParse(paymentReturnPayload.Control, out _));
}
๊ทธ๊ฒ๋ค์ ๋ถ๋ณ์
๋๋ค. ์ด์ ํจ์ฌ ๋ช
ํํด์ก์ต๋๋ค.
๊ทํ์ ๋ต๋ณ๊ณผ ๋ช ๊ฐ์ง ์ถ๊ฐ ํ์ ๊ฐ์ฌ๋๋ฆฝ๋๋ค!