|
1 | 1 | /* |
2 | 2 | JustMock Lite |
3 | | - Copyright © 2010-2015 Progress Software Corporation |
| 3 | + Copyright © 2010-2015,2019 Progress Software Corporation |
4 | 4 |
|
5 | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
6 | 6 | you may not use this file except in compliance with the License. |
@@ -2479,10 +2479,220 @@ void Scope.IImplementable2.Do() |
2479 | 2479 | } |
2480 | 2480 | } |
2481 | 2481 | #endif |
2482 | | - } |
2483 | 2482 |
|
2484 | | - internal abstract class InternalAbstract |
2485 | | - { |
2486 | | - internal abstract string Bar { get; set; } |
| 2483 | + internal abstract class InternalAbstract |
| 2484 | + { |
| 2485 | + internal abstract string Bar { get; set; } |
| 2486 | + } |
| 2487 | + |
| 2488 | + #region Category "FluentConfig" |
| 2489 | + |
| 2490 | + public class Base |
| 2491 | + { |
| 2492 | + public const int DefaultValue = 3; |
| 2493 | + |
| 2494 | + public int i = DefaultValue; |
| 2495 | + |
| 2496 | + public Base(int i) |
| 2497 | + { |
| 2498 | + this.i = i; |
| 2499 | + } |
| 2500 | + } |
| 2501 | + |
| 2502 | + [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")] |
| 2503 | + public void ShouldUseAutoselectedConstructorMockingBehaviorWithFluentGenericConfig() |
| 2504 | + { |
| 2505 | + var proxy = Mock.Create<Base>(fluentConfig => { }); |
| 2506 | + |
| 2507 | + Assert.Equal(default(int), proxy.i); |
| 2508 | + Assert.Null(proxy as IDisposable); |
| 2509 | + } |
| 2510 | + |
| 2511 | + [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")] |
| 2512 | + public void ShouldMockWhenMissingPameterlessConstructorAndRecursiveLooseWithFluentGenericConfig() |
| 2513 | + { |
| 2514 | + var proxy = Mock.Create<Base>(fluentConfig => |
| 2515 | + fluentConfig.SetBehavior(Behavior.RecursiveLoose) |
| 2516 | + ); |
| 2517 | + |
| 2518 | + Assert.Equal(default(int), proxy.i); |
| 2519 | + Assert.Null(proxy as IDisposable); |
| 2520 | + } |
| 2521 | + |
| 2522 | + [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")] |
| 2523 | + public void ShouldMockWhenMissingPameterlessConstructorAndLooseWithFluentGenericConfig() |
| 2524 | + { |
| 2525 | + var proxy = Mock.Create<Base>(fluentConfig => |
| 2526 | + fluentConfig.SetBehavior(Behavior.Loose) |
| 2527 | + ); |
| 2528 | + |
| 2529 | + Assert.Equal(default(int), proxy.i); |
| 2530 | + Assert.Null(proxy as IDisposable); |
| 2531 | + } |
| 2532 | + |
| 2533 | + // Implementation differs for .NETFramework and .NETCore, see DynamicProxyMockFactory.Create method |
| 2534 | +#if !NETCORE |
| 2535 | + [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")] |
| 2536 | + public void ShouldThrowWhenMissingPameterlessConstructorAndCallOriginalWithFluentGenericConfig() |
| 2537 | + { |
| 2538 | + Assert.Throws<MockException>(() => |
| 2539 | + Mock.Create<Base>(fluentConfig => |
| 2540 | + fluentConfig.SetBehavior(Behavior.CallOriginal)) |
| 2541 | + ); |
| 2542 | + } |
| 2543 | +#else |
| 2544 | + [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")] |
| 2545 | + public void ShouldMockWhenMissingPameterlessConstructorAndCallOriginalWithFluentGenericConfig() |
| 2546 | + { |
| 2547 | + var proxy = Mock.Create<Base>(fluentConfig => |
| 2548 | + fluentConfig.SetBehavior(Behavior.CallOriginal) |
| 2549 | + ); |
| 2550 | + |
| 2551 | + Assert.Equal(default(int), proxy.i); |
| 2552 | + Assert.Null(proxy as IDisposable); |
| 2553 | + } |
| 2554 | +#endif |
| 2555 | + |
| 2556 | + [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")] |
| 2557 | + public void ShouldThrowWhenMockConstructorAndCallConstructorWithFluentGenericConfig() |
| 2558 | + { |
| 2559 | + Assert.Throws<MockException>(() => |
| 2560 | + Mock.Create<Base>(fluentConfig => |
| 2561 | + fluentConfig.MockConstructor().CallConstructor(new object[] { 5 })) |
| 2562 | + ); |
| 2563 | + } |
| 2564 | + |
| 2565 | + [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")] |
| 2566 | + public void ShouldThrowWhenCallConstructorAndMockConstructorWithFluentGenericConfig() |
| 2567 | + { |
| 2568 | + Assert.Throws<MockException>(() => |
| 2569 | + Mock.Create<Base>(fluentConfig => |
| 2570 | + fluentConfig.CallConstructor(new object[] { 5 }).MockConstructor()) |
| 2571 | + ); |
| 2572 | + } |
| 2573 | + |
| 2574 | + [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")] |
| 2575 | + public void ShouldSpecifyConstructorArgumentsWithFluentGenericConfig() |
| 2576 | + { |
| 2577 | + var proxy = Mock.Create<Base>(fluentConfig => |
| 2578 | + fluentConfig.CallConstructor(new object[] { 5 }) |
| 2579 | + ); |
| 2580 | + |
| 2581 | + Assert.Equal(5, proxy.i); |
| 2582 | + Assert.Null(proxy as IDisposable); |
| 2583 | + } |
| 2584 | + |
| 2585 | + [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")] |
| 2586 | + public void ShouldMockConstructorWithFluentGenericConfig() |
| 2587 | + { |
| 2588 | + var proxy = Mock.Create<Base>(fluentConfig => |
| 2589 | + fluentConfig.MockConstructor() |
| 2590 | + ); |
| 2591 | + |
| 2592 | + Assert.Equal(default(int), proxy.i); |
| 2593 | + Assert.Null(proxy as IDisposable); |
| 2594 | + } |
| 2595 | + |
| 2596 | + [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")] |
| 2597 | + public void ShouldImplementInterfaceWithFluentGenericConfig() |
| 2598 | + { |
| 2599 | + var proxy = Mock.Create<Base>(fluentConfig => |
| 2600 | + fluentConfig.Implements<IDisposable>() |
| 2601 | + ); |
| 2602 | + |
| 2603 | + Assert.Equal(default(int), proxy.i); |
| 2604 | + Assert.NotNull(proxy as IDisposable); |
| 2605 | + } |
| 2606 | + |
| 2607 | + [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")] |
| 2608 | + public void ShouldUseAutoselectedConstructorMockingBehaviorWithFluentConfig() |
| 2609 | + { |
| 2610 | + var proxy = (Base)Mock.Create(typeof(Base), fluentConfig => { }); |
| 2611 | + |
| 2612 | + Assert.Equal(default(int), proxy.i); |
| 2613 | + } |
| 2614 | + |
| 2615 | + [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")] |
| 2616 | + public void ShouldMockWhenMissingPameterlessConstructorAndRecursiveLooseWithFluentConfig() |
| 2617 | + { |
| 2618 | + var proxy = (Base)Mock.Create(typeof(Base), fluentConfig => |
| 2619 | + fluentConfig.SetBehavior(Behavior.RecursiveLoose) |
| 2620 | + ); |
| 2621 | + |
| 2622 | + Assert.Equal(default(int), proxy.i); |
| 2623 | + } |
| 2624 | + |
| 2625 | + [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")] |
| 2626 | + public void ShouldMockWhenMissingPameterlessConstructorAndLooseWithFluentConfig() |
| 2627 | + { |
| 2628 | + var proxy = (Base)Mock.Create(typeof(Base), fluentConfig => |
| 2629 | + fluentConfig.SetBehavior(Behavior.Loose) |
| 2630 | + ); |
| 2631 | + |
| 2632 | + Assert.Equal(default(int), proxy.i); |
| 2633 | + } |
| 2634 | + |
| 2635 | + // Implementation differs for .NETFramework and .NETCore, see DynamicProxyMockFactory.Create method |
| 2636 | +#if !NETCORE |
| 2637 | + [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")] |
| 2638 | + public void ShouldThrowWhenMissingPameterlessConstructorAndCallOriginalWithFluentConfig() |
| 2639 | + { |
| 2640 | + Assert.Throws<MockException>(() => |
| 2641 | + Mock.Create(typeof(Base), fluentConfig => |
| 2642 | + fluentConfig.SetBehavior(Behavior.CallOriginal)) |
| 2643 | + ); |
| 2644 | + } |
| 2645 | +#else |
| 2646 | + [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")] |
| 2647 | + public void ShouldMockWhenMissingPameterlessConstructorAndCallOriginalWithFluentConfig() |
| 2648 | + { |
| 2649 | + var proxy = (Base)Mock.Create(typeof(Base), fluentConfig => |
| 2650 | + fluentConfig.SetBehavior(Behavior.CallOriginal) |
| 2651 | + ); |
| 2652 | + |
| 2653 | + Assert.Equal(default(int), proxy.i); |
| 2654 | + } |
| 2655 | +#endif |
| 2656 | + |
| 2657 | + [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")] |
| 2658 | + public void ShouldThrowWhenMockConstructorAndCallConstructorWithFluentConfig() |
| 2659 | + { |
| 2660 | + Assert.Throws<MockException>(() => |
| 2661 | + Mock.Create(typeof(Base), fluentConfig => |
| 2662 | + fluentConfig.MockConstructor().CallConstructor(new object[] { 5 })) |
| 2663 | + ); |
| 2664 | + } |
| 2665 | + |
| 2666 | + [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")] |
| 2667 | + public void ShouldThrowWhenCallConstructorAndMockConstructorWithFluentConfig() |
| 2668 | + { |
| 2669 | + Assert.Throws<MockException>(() => |
| 2670 | + Mock.Create(typeof(Base), fluentConfig => |
| 2671 | + fluentConfig.CallConstructor(new object[] { 5 }).MockConstructor()) |
| 2672 | + ); |
| 2673 | + } |
| 2674 | + |
| 2675 | + [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")] |
| 2676 | + public void ShouldSpecifyConstructorArgumentsWithFluentConfig() |
| 2677 | + { |
| 2678 | + var proxy = (Base)Mock.Create(typeof(Base), fluentConfig => |
| 2679 | + fluentConfig.CallConstructor(new object[] { 5 }) |
| 2680 | + ); |
| 2681 | + |
| 2682 | + Assert.Equal(5, proxy.i); |
| 2683 | + } |
| 2684 | + |
| 2685 | + [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")] |
| 2686 | + public void ShouldMockConstructorWithFluentConfig() |
| 2687 | + { |
| 2688 | + var proxy = (Base)Mock.Create(typeof(Base), fluentConfig => |
| 2689 | + fluentConfig.MockConstructor() |
| 2690 | + ); |
| 2691 | + |
| 2692 | + Assert.Equal(default(int), proxy.i); |
| 2693 | + } |
| 2694 | + |
| 2695 | + #endregion |
2487 | 2696 | } |
| 2697 | + |
2488 | 2698 | } |
0 commit comments