2014-10-15 4 views
7

का उपयोग करने वाले यूनिट परीक्षण कोड को कैसे करें मैंने पाया है कि ओविन के पास यह शानदार Microsoft.Owin.Testing लाइब्रेरी है जो आपको अपने वेब एप्लिकेशन को मेमोरी का परीक्षण करने देती है। हालांकि, मेरी साइट को उन संसाधनों तक पहुंचने से पहले प्रमाणीकरण की आवश्यकता है जिनमें जटिल लेखन परीक्षण कोड है।ओवेन कुकी ऑडिनेशन

क्या माइक्रोसॉफ्ट.ऑविन.स्टिंग का उपयोग करते समय प्रमाणीकरण के लिए "मॉक" प्रमाणीकरण का कोई सुविधाजनक तरीका है?

मैं एक आउट-ऑफ-प्रक्रिया एसटीएस हिट करने के लिए की जरूरत नहीं करने के लिए अपने इकाई परीक्षण चाहते हैं और मैं कोड है कि प्रोग्राम के रूप में एक में स्मृति एसटीएस (जैसे Thinktecture.IdentityServer.v3 के रूप में के खिलाफ में संकेत लिखने की ज़रूरत नहीं करना चाहते हैं)।

मेरे द्वारा आने वाला सबसे आसान समाधान यूनिट परीक्षणों के लिए प्रमाणीकरण कोड को अक्षम करना है, जिसमें से मैं प्रशंसक नहीं हूं।

मैं कुकी प्रमाणीकरण के साथ ओपनआईडी कनेक्ट का उपयोग कर रहा हूं। यहां एक निहित उदाहरण है। OpenId कनेक्ट के लिए कॉन्फ़िगरेशन स्ट्रिंग को वास्तविक सर्वर के लिए भरना होगा।

[Test] 
public async void AccessAuthenthicatedResourceTest() 
{ 
    const string ClientId = ""; 
    const string RedirectUri = ""; 
    const string Authority = ""; 

    TestServer server = TestServer.Create(
     app => 
      { 
       //Configure Open ID Connect With Cookie Authenthication 
       app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); 
       app.UseCookieAuthentication(new CookieAuthenticationOptions()); 
       app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions 
        { 
        ClientId = ClientId, 
        RedirectUri = RedirectUri, 
        Authority = Authority 
        }); 

       // Requires Authentication 
       app.Use(
        async (context, next) => 
         { 
          var user = context.Authentication.User; 
          if (user == null 
           || user.Identity == null 
           || !user.Identity.IsAuthenticated) 
          { 
           context.Authentication.Challenge(); 
           return; 
          } 

          await next(); 
         }); 

       app.Run(async context => await context.Response.WriteAsync("My Message")); 
      }); 


    //Do or Bypass authenthication 

    HttpResponseMessage message = await server.CreateRequest("/").GetAsync(); 

    Assert.AreEqual("My Message", await message.Content.ReadAsStringAsync()); 
} 

उत्तर

0

मुझे लगता है कि मजाक अपने नियंत्रक में कोड का एक हिस्सा परीक्षण करने के लिए है। आप नकली डेटा का उपयोग नकली डेटा इंजेक्ट कर सकते हैं। आपको उपयोगकर्ता प्रदाता के लिए इंटरफ़ेस बनाना होगा।

public interface IUserProvider 
    { 
     string GetUserId(); 
     string GetUserName(); 
    } 

और अपने आधार वर्ग के लिए यह इंजेक्षन:

protected BaseController(IUnitOfWork data, IUserProvider userProvider) 
     { 
      this.data = data; 
      this.userProvider = userProvider; 
     } 

उसके बाद आप तो जैसे IUserProvider नकली कर सकते हैं:

var userMockReposioty = new Mock<IRepository<ApplicationUser>>(); 
      var userMockUserProvider = new Mock<IUserProvider>(); 
      userMockUserProvider.Setup(x => x.GetUserName()) 
       .Returns("FakeUserName"); 

      userMockUserProvider.Setup(x => x.GetUserId()) 
       .Returns("c52b2a96-8258-4cb0-b844-a6e443acb04b"); 

mockUnitOfWork.Setup(x => x.Users).Returns(userMockReposioty.Object); 

मुझे आशा है कि यह आप में मदद मिलेगी।