๋ด AutoMapper ๋งคํ์ด ํญ๋ฐํ์ง ์๋๋ก ํ๊ธฐ ์ํด ๋ค์๊ณผ ์ ์ฌํ ํ ์คํธ ํด๋์ค๊ฐ ์์ต๋๋ค.
public class MapperTests
{
private readonly IMapper _mapper; // from AutoMapper
private readonly IFixture _fixture;
public MapperTests()
{
_mapper = SetUpAutoMapper();
_fixture = new Fixture();
}
[Theory]
[InlineData(typeof(Foo), typeof(Bar))]
[InlineData(typeof(Boo), typeof(Far))]
// etc ...
public void CanMapTypes(Type inputType, Type outputType)
{
var input = _fixture.Create(inputType);
// throws exceptions if something's broken:
_mapper.Map(input, outputType)
}
}
๋ ๊ทธ๋ฅ ์ฌ์ค์ ๋ฐ๊ฒฌ _fixture.Create(inputType)
์ค์ ๋ก ํ์์ ์ธ์คํด์ค๋ฅผ ์์ฑํ์ง ์์ต๋๋ค (์ : Foo
์ฒซ ๋ฒ์งธ ํ
์คํธ์์) ์ค์ ๋ก์ ์ธ์คํด์ค System.Type
. ๋ฌผ๋ก ์ด๊ฒ์ ๋ด ํ
์คํธ๋ฅผ ์๋นํ ์ธ๋ชจ ์๊ฒ ๋ง๋ญ๋๋ค.
์ฌ์ฉ ๊ฐ๋ฅํ ๋ฐฉ๋ฒ(IntelliSense ํ์)์ ์ดํด๋ณด์์ง๋ง ๋ด ๊ธฐ๋์ ์ผ์นํ๋ ๊ฒ์ผ๋ก ๋ณด์ด๋ ๋ฐฉ๋ฒ์ ์ฐพ์ ์ ์์์ต๋๋ค. ๋ด๊ฐ ์ํ๋ ๊ฒ์ ํ ์์๋ ๋ฐฉ๋ฒ์ด ์์ต๋๊น? ๊ธฐ๋ณธ์ ์ผ๋ก ๋ฐํ์์ ์ ๊ณต๋๋ ์ ํ์ ์ธ์คํด์ค๋ฅผ ์์ฑํ์๊ฒ ์ต๋๊น?
๋ฌด์จ ์ผ์ธ์ง ์ ๊ฒ ๊ฐ์ต๋๋ค ๐
AutoFixture v4 ๋ฅผ ์๋ํ๋ฉด ์ด ์ฝ๋๊ฐ ์ปดํ์ผ๋์ง ์๋๋ค๋ ๊ฒ์ ์๊ฒ ๋ ๊ฒ์
๋๋ค. ๊ทธ ์ด์ ๋ ํผ๋์ค๋ฌ์ด Create()
๋ฉ์๋ ์ค๋ฒ๋ก๋ ๋ฅผ ๋ณ๋์ ํจํค์ง๋ก
```c#
_fixture.Create(์
๋ ฅ ์ ํ);
It might look that you are making a request of `typeof(Foo)` here, however in reality you are passing this argument as a `seed` and the actual request type is `typeof(Type)`. To fix the issue please tune a bit your code:
```c#
public class Foo
{
}
[Theory]
[InlineData(typeof(Foo))]
public void TestTypeRequest(Type requestType)
{
var fixture = new Fixture();
var result = fixture.Create(requestType, new SpecimenContext(fixture));
Assert.IsAssignableFrom<Foo>(result);
}
API๋ ๊ฐ๊ฒฐํ์ง ์์ ์ ์์ง๋ง ์๋๋ฆฌ์ค๊ฐ ์ผ๋ฐ์ ์ด์ง ์์ผ๋ฏ๋ก ๊ด์ฐฎ์ต๋๋ค.
๋์์ด ๋์๋์ง ์๋ ค์ฃผ์ธ์ ๐
๊ฐ์ฌ ํด์!
์ผ๋ฐ fixture.Create<T>()
๊ฐ์ฒด์ ์์ค ์ฝ๋๋ฅผ ์ฝ๊ณ ๊ฒฐ๊ตญ ์ด๋์ ์๋์ง ํ์
ํ์ฌ ์ด๊ฒ์ด ์๋ํ๋ค๋ ๊ฒ๋ ์์์ต๋๋ค.
var input = new SpecimenContext(_fixture).Resolve(sourceType);
๋๋ ๊ทธ๋ค์ด ๋ค์ ๋๋ฑํ๋ค๊ณ ์๊ฐํฉ๋๋ค :) ์ ๋ง ๊ฐ์ฌํฉ๋๋ค!
@tlycken ์ค์ ๋ก ๋ ์ค๋ํซ ๋ชจ๋ ๋์ผํ ์์ ์ ์ํํ์ง๋ง ์ต์ ์ ํจ์ฌ ๋ ์ข์ ๋ณด์ ๋๋ค :) ํ์ํ ๊ฒฝ์ฐ ๋์ค์ ์ฌ์ฉํ ๊ฒ์ ๋๋ค :blush:
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
๊ฐ์ฌ ํด์!
์ผ๋ฐ
fixture.Create<T>()
๊ฐ์ฒด์ ์์ค ์ฝ๋๋ฅผ ์ฝ๊ณ ๊ฒฐ๊ตญ ์ด๋์ ์๋์ง ํ์ ํ์ฌ ์ด๊ฒ์ด ์๋ํ๋ค๋ ๊ฒ๋ ์์์ต๋๋ค.๋๋ ๊ทธ๋ค์ด ๋ค์ ๋๋ฑํ๋ค๊ณ ์๊ฐํฉ๋๋ค :) ์ ๋ง ๊ฐ์ฌํฉ๋๋ค!