dennisroche / xunit.ioc.autofac

XUnit2 Test Framework implementation with Autofac
MIT License
20 stars 9 forks source link

does this work ? #13

Closed sgf closed 6 years ago

sgf commented 6 years ago

im test much times,its do not work.im not sure whats the problem?

[assembly: TestFramework("Penseesoft.Recruitment.xTest.ConfigureTestFramework", "AssemblyName")]
namespace Penseesoft.Recruitment.xTest
{
    public class ConfigureTestFramework : AutofacTestFramework
    {
        private ContainerBuilder RegisterService()
        {
            var builder = new ContainerBuilder();
            var assemblys = AppDomain.CurrentDomain.GetAssemblies();//.Where(asm => asm.FullName.StartsWith("Penseesoft") && asm.FullName.EndsWith("BLL"));
            builder.RegisterAssemblyTypes(assemblys.ToArray())//
                .Where(type => type.Name.EndsWith("Tests") || type.Name.EndsWith("Service")).AsSelf()
                   .AsImplementedInterfaces().PropertiesAutowired().InstancePerLifetimeScope();

            builder.Register(context => new TestOutputHelper())
               .AsSelf()
               .As<ITestOutputHelper>()
               .InstancePerLifetimeScope();
            return builder;
        }

        public ConfigureTestFramework(IMessageSink diagnosticMessageSink)
            : base(diagnosticMessageSink)
        {
            var builder = RegisterService();
            builder.Register(context => new TestOutputHelper())
                .AsSelf()
                .As<ITestOutputHelper>()
                .InstancePerLifetimeScope();
            Container = builder.Build();
        }
    }
}

 [UseAutofacTestFramework]
    public class TinyOrmTests
    {
        private IRequestManageService _requestManageService;
        private readonly ITestOutputHelper _classHelper;
        public TinyOrmTests(IRequestManageService requestManageService, ITestOutputHelper classHelper)
        {
            _requestManageService = requestManageService;
            _classHelper = classHelper;
        }
        [Fact]
        public void FactTest()
        {
            _classHelper.WriteLine("ClassHelperInFact");
        }

        [Theory]//多种参数一起参与测试
        [InlineData("0003" )]
        public void TestMethod1(string staff)
        {
            var pagingInfo =new PagingInfo() { RowCount = 0, PageIndex = 0, PageSize = 0, OrderField = "" };
            _requestManageService.GetRequestPositionsByStaffNo(pagingInfo, staff);
            _classHelper.WriteLine("");
        }
    }

the Test Always output: The following constructor parameters did not have matching fixture data: IRequestManageService requestManageService

sgf commented 6 years ago

Did I missed something?

dennisroche commented 6 years ago

@sgf that error message is because IRequestManageService cannot be resolved from the AutoFac container.

To verify this, try adding an explicit registered for IRequestManageService, e.g.

builder.Register(context => new FakeRequestManageService())
               .AsSelf()
               .As<IRequestManageService>()
               .InstancePerLifetimeScope();

It could also be a bug with the implementation; if so try the recent fork https://github.com/pizycki/xunit.ioc.autofac

sgf commented 6 years ago

@dennisroche thank u reply,still do not work. im create a new TestInterface:

   public interface ITestInterface
    {
        void Msg(string msg);
    }

    public class TestInterface : ITestInterface
    {
        public void Msg(string msg)
        {
            Console.WriteLine(msg);
        }
    }
//and im do it like this:
     builder.Register(context => new TestInterface())
               .AsSelf()
               .As<ITestInterface>()
               .InstancePerLifetimeScope();

i will try recent fork thank u.

sgf commented 6 years ago

@dennisroche maybe the reason is what im using the Nuget package.

dennisroche commented 6 years ago

Sorry I couldn't help more.