ruby on rails - Link doesn't hide after some time capybara -


i use capybara test code located in comment model (5 minutes conventional):

 def editable?     self.created_at < (time.now - 5.minute)  end 

the view of link:

- unless comment.editable?   = link_to 'edit', edit_category_theme_comment_path(@category, @theme, comment) 

so after 5 minutes link edit must hide page (but need refresh page). here code in rspec creating comments , testing link-hide functionality:

 def create_comment(options={})    options[:content] ||= 'i comment'     visit category_theme_path(category, theme)    within '.comment-form'      fill_in 'content', with: options[:content]      click_button 'submit'    end  end   context 'comment has content'    before(:each) { create_comment }     'hides edit symbol due 5 minutes after comment created'      using_wait_time 400        visit category_theme_path(category, theme)        save_and_open_page        expect(page).to have_no_css('.comment-edit')      end    end   end 

but got: failure/error: expect(page).to have_no_css('.comment-edit')expected #has_no_css?(".comment-edit") return true, got false

i try use page.reload!, expect(page).to have_no_css('.comment-edit', wait: 400) , other related staff, capybara don't want wait. maybe use using_wait_time wrong place, if - how can test this?

enter image description here

there number of things wrong approach testing this. reason attempt failing because using_wait_time sets amount of time capybaras matchers wait expectations become true, doesn't make program wait. therefore expect(page).to have_no_css('.comment-edit') wait time using_wait_time specifies, rechecking page every 50 milliseconds or content, never reloads page , doesn't wait before loading page. approach work need sleep before visiting page

it 'hides edit symbol due 5 minutes after comment created'   sleep 5.minutes         visit category_theme_path(category, theme)   save_and_open_page   expect(page).to have_no_css('.comment-edit') end 

however terrible idea since tests become ridiculously slow.

rather approach generating test comment "old" (as pascal betz suggests), using factorygirl , specifying created_at more 5 minutes ago.

factorygirl.create(:comment, created_at: 5.minutes.ago) 

or if want continue creating comment through interface include timecop gem , can do

 timecop.travel(5.minutes.from_now)    visit category_theme_path(category, theme)    save_and_open_page    expect(page).to have_no_css('.comment-edit')  end 

which move clock 5 minutes forward before visiting page, , reset normal once block if finished.

additionally, editable? method comparing in wrong direction , should be

def editable?   self.created_at > (time.now - 5.minute) end 

then view should

- if comment.editable?   = link_to 'edit', edit_category_theme_comment_path(@category, @theme, comment) 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -