This project has moved and is read-only. For the latest updates, please go here.

Unit Testing

Sep 13, 2013 at 8:47 PM
I am building a web application in MVC and I want to include a unit testing project. I'm not sure how to create the twitter context object, I don't want to redirect to the authentication page because I want the unit test to run without intervention. Here is my code:

public class UserInfoTest
    public TwitterContext GetTwitterContext()
        var auth = new MvcAuthorizer
            Credentials = new InMemoryCredentials()
                ConsumerKey = "xxx",
                ConsumerSecret = "xxx",
                AccessToken = "xxx"
        var twitterCtx = new TwitterContext(auth);

        return twitterCtx;

    public void GetUser()
        TwitterContext twitterCtx = GetTwitterContext();
        var tUser =
                (from user in twitterCtx.User
                 where user.Type == UserType.Lookup &&
                       user.UserID == "123456789"
                 select user).FirstOrDefault();

When I run this I get an an error executing my query, the error says "Bad Authentication Data". Can anyone tell me how I can unit test using LinqToTwitter?
Sep 20, 2013 at 10:25 PM

I have a project in the downloadable source code called LinqToTwitterXUnitTests with tests for TwitterContext in the LinqToTwitterTests folder. I've added constructor overloads to TwitterContext to facilitate unit testing. The ITwitterExecute interface type performs the actual query/command to Twitter, so I mock it and use the matching TwitterContext overload. Here's how I initialize the TwitterContext in my tests:
        void InitializeTwitterContextForExecuteTest(out TwitterContext ctx, out Expression expression)
            var exec = new Mock<ITwitterExecute>();
            exec.Setup(exc => exc.QueryTwitter(It.IsAny<Request>(), It.IsAny<IRequestProcessor<Status>>()))

            ctx = new TwitterContext(exec.Object);
            var publicQuery =
                from tweet in ctx.Status
                where tweet.Type == StatusType.Show
                select tweet;

            expression = publicQuery.Expression;
I'm using Moq and XUnit. Feedback on this and any other unit tests I've set up is welcome.

Sep 23, 2013 at 2:16 PM
That's awesome! Thanks for the reply. I'll download that right now.