Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
//-------------------------------------------------------------------------------

//-------------------------------------------------------------------------------

using Ninject.Extensions.Conventions.Fakes;

#if !NO_MOQ
namespace Ninject.Extensions.Conventions.BindingBuilder
{
Expand Down Expand Up @@ -210,6 +212,57 @@ public void Configure()
{
bindingMock.Verify(b => b.InSingletonScope());
}
}

[Fact]
public void ConfigureScopesFromAttributes()
{
var types = new[] { typeof(SingletonScopedService), typeof(ThreadScopedService), typeof(TransientScopedService) };
var generatorMock = new Mock<IBindingGenerator>();
var singletonScopedBindingMocks = new[] { CreateBindingMock(), CreateBindingMock() };
var threadScopedBindingMocks = new[] { CreateBindingMock(), CreateBindingMock() };
var transientScopedBindingMocks = new[] { CreateBindingMock(), CreateBindingMock() };

this.SetupTypeFilterGetTypes(types);
generatorMock.Setup(g => g.CreateBindings(typeof(SingletonScopedService), this.bindingRoot)).Returns(singletonScopedBindingMocks.Select(b => b.Object));
generatorMock.Setup(g => g.CreateBindings(typeof(ThreadScopedService), this.bindingRoot)).Returns(threadScopedBindingMocks.Select(b => b.Object));
generatorMock.Setup(g => g.CreateBindings(typeof(TransientScopedService), this.bindingRoot)).Returns(transientScopedBindingMocks.Select(b => b.Object));

this.testee.SelectAllTypesFrom(new Assembly[0]);
this.testee.BindWith(generatorMock.Object);
this.testee.ConfigureScopesFromAttributes();

foreach (var bindingMock in singletonScopedBindingMocks)
{
bindingMock.Verify(b => b.InSingletonScope());
}

foreach (var bindingMock in threadScopedBindingMocks)
{
bindingMock.Verify(b => b.InThreadScope());
}

foreach (var bindingMock in transientScopedBindingMocks)
{
bindingMock.Verify(b => b.InTransientScope());
}
}

[Fact]
public void ConfigureScopesFromAttributesThrowsIfServiceHasMultipleScopes()
{
var types = new[] { typeof(ServiceWithMultipleScopes) };
var generatorMock = new Mock<IBindingGenerator>();
var bindingMocks = new[] { CreateBindingMock(), CreateBindingMock() };

this.SetupTypeFilterGetTypes(types);
generatorMock.Setup(g => g.CreateBindings(typeof(ServiceWithMultipleScopes), this.bindingRoot)).Returns(bindingMocks.Select(b => b.Object));

this.testee.SelectAllTypesFrom(new Assembly[0]);
this.testee.BindWith(generatorMock.Object);


Assert.Throws<InvalidOperationException>(() => this.testee.ConfigureScopesFromAttributes());
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//-------------------------------------------------------------------------------
// <copyright file="ServiceWithMultipleScopes.cs" company="Ninject Project Contributors">
// Copyright (c) 2009-2011 Ninject Project Contributors
// Authors: Dominik Schlosser (dominik.schlosser@gmail.com)
//
// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
// you may not use this file except in compliance with one of the Licenses.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// or
// http://www.microsoft.com/opensource/licenses.mspx
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
//-------------------------------------------------------------------------------

using Ninject.Extensions.Conventions.Attributes;

namespace Ninject.Extensions.Conventions.Fakes
{
[TransientScoped]
[ThreadScoped]
[SingletonScoped]
public class ServiceWithMultipleScopes
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//-------------------------------------------------------------------------------
// <copyright file="SingletonScopedService.cs" company="Ninject Project Contributors">
// Copyright (c) 2009-2011 Ninject Project Contributors
// Authors: Dominik Schlosser (dominik.schlosser@gmail.com)
//
// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
// you may not use this file except in compliance with one of the Licenses.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// or
// http://www.microsoft.com/opensource/licenses.mspx
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
//-------------------------------------------------------------------------------

using Ninject.Extensions.Conventions.Attributes;

namespace Ninject.Extensions.Conventions.Fakes
{
[SingletonScoped]
public class SingletonScopedService : ThreadScopedService
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//-------------------------------------------------------------------------------
// <copyright file="ThreadScopedService.cs" company="Ninject Project Contributors">
// Copyright (c) 2009-2011 Ninject Project Contributors
// Authors: Dominik Schlosser (dominik.schlosser@gmail.com)
//
// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
// you may not use this file except in compliance with one of the Licenses.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// or
// http://www.microsoft.com/opensource/licenses.mspx
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
//-------------------------------------------------------------------------------

using Ninject.Extensions.Conventions.Attributes;

namespace Ninject.Extensions.Conventions.Fakes
{
[ThreadScoped]
public class ThreadScopedService
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//-------------------------------------------------------------------------------
// <copyright file="TransientScopedService.cs" company="Ninject Project Contributors">
// Copyright (c) 2009-2011 Ninject Project Contributors
// Authors: Dominik Schlosser (dominik.schlosser@gmail.com)
//
// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
// you may not use this file except in compliance with one of the Licenses.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// or
// http://www.microsoft.com/opensource/licenses.mspx
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
//-------------------------------------------------------------------------------

using Ninject.Extensions.Conventions.Attributes;

namespace Ninject.Extensions.Conventions.Fakes
{
[TransientScoped]
public class TransientScopedService
{
}
}
Loading