Using NSpec at various architectural layers

Having read the quick start at nspec.org, I realized that NSpec might be a useful tool in a scenario which was becoming a bit cumbersome with NUnit alone.

I’m adding an OAuth (or, DotNetOpenAuth) to a website and quickly made a mess of writing test methods such as

[Test]
public void UserIsLoggedInLocallyPriorToInvokingExternalLoginAndExternalLoginSucceedsAndExternalProviderIdIsNotAlreadyAssociatedWithUserAccount()
{
...
}

… and I wound up with maybe a dozen permutations of this theme, for the user already being logged in locally and not locally, the external login succeeding or failing, etc. Not only were the method names unwieldy, but every test needed a setup that contained parts in common with a different set of other tests.

I realized that NSpec’s incremental setup capabilities would work great for this, and for a while I was trucking a long wonderfully, with code like

  act = () => { actionResult = controller.ExternalLoginCallback(returnUrl); };

            context["The user is already logged in"] = () =>
            {
                before = () => identity.Setup(x => x.IsAuthenticated).Returns(true);
                context["The external login succeeds"] = () =>
                {
                    before = () => oauth.Setup(x => x.VerifyAuthentication(It.IsAny<string>())).Returns(new AuthenticationResult(true, providerName, "provideruserid", "username", new Dictionary<string, string>()));
                    context["External login already exists for current user"] = () =>
                    {
                        before = () => authService.Setup(x => x.ExternalLoginExistsForUser(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(true);
                        it["Should add 'login sucessful' alert"] = () =>
                        {
                            var alerts = (IList<Alert>)controller.TempData[TempDataKeys.AlertCollection];
                            alerts[0].Message.should_be_same("Login successful");
                            alerts[0].AlertType.should_be(AlertType.Success);
                        };
                        it["Should return a redirect result"] = () => actionResult.should_cast_to<RedirectToRouteResult>();
                    };
                    context["External login already exists for another user"] = () =>
                    {
                        before = () => authService.Setup(x => x.ExternalLoginExistsForAnyOtherUser(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(true);
                        it["Adds an error alert"] = () =>
                        {
                            var alerts = (IList<Alert>)controller.TempData[TempDataKeys.AlertCollection];
                            alerts[0].Message.should_be_same("The external login you requested is already associated with a different user account");
                            alerts[0].AlertType.should_be(AlertType.Error);
                        };
                        it["Should return a redirect result"] = () => actionResult.should_cast_to<RedirectToRouteResult>();
                    };

This approach seemed to work magnificently until I prepared to write test code for my ApplicationServices layer, to which I delegate viewmodel manipulation from my MVC controllers, and which coordinates the operations of the lower data repository layer:

public void CreateUserAccountFromExternalLogin(RegisterExternalLoginModel model)
{
    throw new NotImplementedException();
}

public void AssociateExternalLoginWithUser(string userName, string provider, string providerUserId)
{
    throw new NotImplementedException();
}

public string GetLocalUserName(string provider, string providerUserId)
{
    throw new NotImplementedException();
}

I have no idea what in the world to name the test class, the test methods, or even if I should perhaps include the testing for this layer into the test class from my large code snippet above, so that a single feature or user action could be tested without regard to architectural layering.

I can’t find any tutorials or blog posts which cover more than simple examples, so I would appreciate any recommendations or pointing in the right direction. I would even welcome “your question is invalid”-type answers as long as some explanation is provided.

Beware: I’ve never actually used NSpec, but I use it’s cousin RSpec all the time. It’s my experience with that tool that leads me to the following answer.

I suggest using this naming convention:

class describe_your_class : nspec
{
  void instance_method_CreateUserAccountFromExternalLogin()
  {
    before = () => 
    {
      account_repository = new AccountRepository();
      fixture = new YourClass(account_repository);
    }
    context["with a valid external login model"] = () =>
    {
      before = () => 
      {
        account = CreateValidAccountForTesting();
        fixture.CreateUserAccountFromExternalLogin(account);
      }
      it["creates a user account"] = () => account_repository.count.should_be(1);
    }
    context["with a null external login model"] = () =>
    {
      before = () =>
      {
        fixture.CreateUserAccountFromExternalLogin(null);
      }
      it["silently does not create a user"] = () => account_repository.count.should_be(0);
    }
  }
}

In RSpec this would look like:

describe YourClass do
  # "#method_name" is a convention that's used in Ruby docs to refer to an
  # instance method. ".method_name" would refer to a class method
  describe '#create_user_account_from_external_login' do
    before do
      # the "@" here indicates that these are instance variables
      @account_repository = AccountRepository.new
      @fixture = YourClass.new(@account_repository)
    end

    context 'with a valid external login' do
      before do
        account = create_valid_account_for_testing()
        @fixture.create_user_account_from_external_login(account)
      end
      it 'creates a user account' do
        @account_repository.count.should == 1
      end
    end

    context 'with a nil external login' do
      before do
        @fixture.create_user_account_from_external_login(nil)
      end
      it 'silently does not create a user account' do
        @account_repository.count.should == 0
      end
    end
  end
end

There are other constructs that RSpec provides, so I probably wouldn’t write my spec exactly like the one above, but I would be able to understand someone else’s that I came across. There’s a great RSpec focused site called Better Specs that might give you more inspiration. You’ll have to translate some of the syntax, of course.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật