1
votes

I need to check if the behavior of my command method is doing a checkout correctly. For this I am trying to mock:

  • A repository;
  • Two branches;

The two branches are one to be the current branch and the other to be the future branch.

The repository will have the two branches mentioned before and I will perform a checkout from one branch to another.

For this I am using NSpec as my unit test suite, moq as my mock framework, libgit2sharp as version control and fluentAssertions to the expectations.

But this (supposed to be) simple task is getting closer to an impossible one to me. I think that is because my lack of depth in using those tools (libgit2sharp and moq). I searched around the web a lot before come here. So, I think this is not duplicated question.

One of the problems I am facing is when I mock (with success) a repository, but when I try add branches (either by calling repo.Branches.Add or repo.CreateBranch) I get the error that the mocked object don't have those methods. And it is correct, because they are extensions methods and I found out that moq have a problem with static methods.

Well, there it goes:

How to mock a repository where exists 2 branches, one of them being the current one, to be possible perform a checkout and switch from the current branch to the other using libgit2sharp and moq?

The code currently seems something like this:

    it["Executa mudança de branches"] = () =>
    {
        var repository = new Mock<IRepository>();
        repository.Setup(rp => rp.Branches.Add("confidence", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644", false));
        repository.Setup(rp => rp.Branches.Add("goodDesign", "be3563ae3f795b2b4353bcce3a527ad0a4f7f645", false));

        repository.Object.CreateBranch("confidence", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
        repository.Object.CreateBranch("goodDesign", "be3563ae3f795b2b4353bcce3a527ad0a4f7f645");

        repository.Object.Checkout(rep.Object.Branches["goodDesign"]);

        repository.Object.Head.Name.Should().Be("goodDesign");

        //my class in test 
        var versionControl = new VersionControl(repository);

        versionControl.ChangeToBranch("confidence");
        versionControl.CurrentBranch.Should().Be("confidence");

    };

And, if it is not possible to do with those tools. How can I do it?

OH, I forgot to say that I am not an English native speaker, so I am really sorry for any mistakes that I certainly did here.

1
Have you looked at the libgit2sharp testing suite, they have Moq examples: github.com/libgit2/libgit2sharp/search?utf8=✓&q=moq - SushiHangover
Yeah, I looked there a lot. I have found a LibGit2Sharp.Tests/RepositoryExtensionsFixture.cs (github.com/libgit2/libgit2sharp/pull/182/files) but is an old code, seems to have been deleted. There is an example similar to what I did, but that strategy don't work. - Daniel

1 Answers

0
votes

I got some luck and wrapped the whole client, so I was able to expose Branches not as BracnCollection, but as IEnumerable<Branch>:

public IEnumerable<Branch> GetBranches() {
    return repository.Branches.AsEnumerable();
}

Then you can mock the collection and Branch class as well.