2017-10-05 15 views
0

在我的測試文件:的Django和Python的AttributeError的: '零售商' 對象有沒有屬性

class TestMakeSoup(TestCase): 
    fixtures = ['deals_test_data.json'] 

    def test_string_is_valid(self): 
     s = Retailer.objects.get(pk=1) 
     with open('/home/danny/PycharmProjects/askarby/deals/tests/BestBuyTest.html', 'r') as myfile: 
      text = myfile.read().replace('\n', '') 
    self.assertTrue(s.make_soup(text)) 

在文件中它的測試:

class retailer(): 
    ''' 
    Retail site, drawn from database queryset object 
    ''' 
    def __init__(self,r_object): 
     ''' 
     Initializes retailer variables 
     obj -> nonetype 

     Precondition: r_object.currency == 3 
     Precondition: r_object.name != None 
     ''' 
     assert len(r_object.currency) == 3, "{} must be a three-letter string (eg 'USD').".format(r_object.currency) 
     assert r_object.name != None, "Name must exist." 
     assert r_object.deal_container_css != None, "Title css must exist." 
     assert r_object.title_css != None, "Title css must exist." 
     assert r_object.price_css != None, "Price css must exist." 
     self.name = r_object.name 
     self.base_url = r_object.base_url 
     self.currency = r_object.currency 
     #dict containing css lookup values for various fields 
     self.css = {} 
     self.css['container'] = self.extract_css(r_object.deal_container_css) 
     self.css['title'] = self.extract_css(r_object.title_css) 
     self.css['product_model'] = self.extract_css(r_object.product_model_css) 
     self.css['price'] = self.extract_css(r_object.price_css) 
     self.css['old_price'] = self.extract_css(r_object.old_price_css) 
     self.css['brand'] = self.extract_css(r_object.brand_css) 
     self.css['image'] = self.extract_css(r_object.image_css) 
     self.css['description'] = self.extract_css(r_object.description_css) 
     self.css['exclude'] = self.extract_css(r_object.exclude_css) 
     self.css['shipping'] = self.extract_css(r_object.shipping_css) 

     #dict containing associated clearance urls for retailer 
     self.clearance_urls = self.get_clearance_urls() 

     #dict to house final list of deals 
     self.deals = {} 


    def __str__(self): 
     return self.name 

    def make_soup(self, text): 
     assert isinstance(text,str), "text must be string." 
     soup = bs4.BeautifulSoup(text, "html.parser") 
     if soup: 
      return soup 
     return False 

Retailer呼叫是指Retailer模型在我交易應用。 我得到這個錯誤:

Error 
Traceback (most recent call last): 
    File "/home/danny/PycharmProjects/askarby/deals/tests/test_deals.py", line 101, in test_string_is_valid 
    self.assertTrue(s.make_soup(text)) 
AttributeError: 'Retailer' object has no attribute 'make_soup' 

爲什麼不make_soup運行的方法?

+0

檢查classname,'make_soup'實際上存在於'Retailer'類中嗎? –

+0

是的。問題在於我實際上並沒有創建課程。 – RubyNoob

回答

0

零售商類採用從數據庫檢索到的對象。我從數據庫中檢索了對象,但沒有用它創建類。

def test_makesoup(self): 
    z = Retailer.objects.get(pk=1) 
    s = dealscan.retailer(z) 
    with open('/home/danny/PycharmProjects/askarby/deals/tests/BestBuyTest.html', 'r') as myfile: 
     text = myfile.read().replace('\n', '') 
    self.assertTrue(s.make_soup(text)) 

解決了它。

相關問題