This project is read-only.

because_of is not being excecuted

Nov 26, 2014 at 12:28 PM
Edited Nov 26, 2014 at 12:30 PM
I installed NBehave and NUnit along with NBehave Spec NUnit and NBehave Fluent in my small project using Manage Nuget Packages....

I'm noticing that the code inside Because_of is not being excecuted, Why?
    public class when_working_with_the_item_type_repository : Specification {

    }

    public class and_saving_a_valid_item_type : when_working_with_the_item_type_repository {
        private int _result;
        private IItemTypeRepository _itemTypeRepository;
        private ItemType _testItemType;
        private int _itemTypeId;

        protected override void Because_of() {
            _result = _itemTypeRepository.Save(_testItemType);
        }
        
        [Test]
        public void then_a_valid_item_type_id_should_be_returned() {
            _result.ShouldEqual(_itemTypeId);
        }
    }
The previous code should fail, but it is succeding!

Thanks in advance,

Rafael
Nov 27, 2014 at 8:45 AM
Hm, not sure whats going on in your code.
How do you run the tests?

I tried this:
using NBehave.Spec.NUnit;
using NUnit.Framework;

namespace SpecIssue
{
    public class Specification : SpecBase<IItemTypeRepository>
    {
        protected override IItemTypeRepository Establish_context()
        {
            return new ItemTypeRepo();
        }
    }

    public class ItemTypeRepo: IItemTypeRepository
    {
        public int Save(ItemType itemType)
        {
            return 0;
        }
    }

    public class when_working_with_the_item_type_repository : Specification
    {
    }

    public class and_saving_a_valid_item_type : when_working_with_the_item_type_repository
    {
        private int _result = -1;
        private ItemType _testItemType;

        protected override void Because_of()
        {
            _result = Sut.Save(_testItemType);
        }

        [Test]
        public void then_a_valid_item_type_id_should_be_returned()
        {
            _result.ShouldEqual(1);
        }
    }

    public class ItemType
    {
    }

    public interface IItemTypeRepository
    {
        int Save(ItemType itemType);
    }
}
And it fails with "Expected: 1 But Was 0", if Because_of hadn't been called the test would pass.
I ran the test with Resharper 8.2.1
Nov 27, 2014 at 4:05 PM
Edited Dec 1, 2014 at 5:25 PM
I'm using Resharper 8.1 Full Edition.

I tested your code and it worked, but why? Is it because it uses SpecBase<IItemTypeRepository> instead of simply SpecBase?

I'm studying TDD and BDD, and I'm following along the book's exercises. the current exercise uses plain SpecBase.

It should work with both.

What can I do to use plain SpecBase?
May 18, 2015 at 8:48 PM
Make sure you decorate your Specification class as a TestFixture

[TestFixture]
public class Specification : SpecBase
{

}