2014-05-25 64 views
0

我在Rails教程的第9章(這一節在9.2.2節)中被卡住了(再次!)。我越來越Rails教程:未定義的方法

bundle exec rspec spec/ 
................................FFF........................ 

Failures: 

1) Authentication authorization as wrong user submitting a GET request to the Users#edit action 
Failure/Error: before {sign_in user, no_capybara: true} 
NoMethodError: 
    undefined method `new_remember_token' for #<User:0x007f8181815448> 
# ./spec/support/utilities.rb:13:in `sign_in' 
# ./spec/requests/authentication_pages_spec.rb:71:in `block (4 levels) in <top (required)>' 

其他2個錯誤是相同的類型。

下面是規範導致錯誤:

describe "as wrong user" do 
     let(:user) {FactoryGirl.create(:user)} 
     let(:wrong_user) {FactoryGirl.create(:user, email: "[email protected]")} 
     before {sign_in user, no_capybara: true} 

     describe "submitting a GET request to the Users#edit action" do 
     before {get edit_user_path(wrong_user)} 
     specify { expect(response.body).not_to match(full_title('Edit user'))} 
     specify { expect(response).to redirect_to(root_url)} 
     end 

     describe "submitting a PATCH request to the Users#update action" do 
     before { patch user_path(wrong_user)} 
     specify { expect(response).to redirect_to(root_url)} 
     end 
    end 

這裏是錯誤消息抱怨的方法(utilities.rb):

def sign_in (user, options={}) 
    if options[:no_capybara] 
    # Sign in when not using Capybara 
    remember_token = User.new_remember_token 
    cookies[:remember_token] 
    user.update_attribute(:remember_token, User.digest(remember_token)) 
    else 
    visit signin_path 
    fill_in "Email", with: user.email 
    fill_in "Password", with: user.password 
    click_button "Sign in" 
    end 
end 

的模型(用戶代碼。 rb)在這裏:

class User < ActiveRecord::Base 
before_save { self.email = email.downcase} 
before_create :create_remember_token 
validates :name, presence: true, length: { maximum: 50 } 
VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false } 
validates :password, length: {minimum: 6} 
has_secure_password 

def User.new_remember_token 
    SecureRandom.urlsafe_base64 
end 

def User.digest(token) 
    Digest::SHA1.hexdigest(token.to_s) 
end 

private 
    def create_remember_token 
    self.remember_token = User.digest(User.new_remember_token) 
    end 
end 

我以前有與sign_in方法的麻煩,但它miraculously disappeared。我究竟做錯了什麼?

回答

0

我終於找到了我在這種情況下觀察到的不穩定測試結果的罪魁禍首,並且很可能在之前的場合(Failure/Error: sign_in user undefined method `sign_in',Rails named route not recognized)。問題似乎是,rails不會在測試之間默認清除緩存。實際上,這真是令人害怕。看來你不能真正相信測試結果。我通過評論Rail的抱怨和重新運行測試的方法來實現這一點。錯誤依然存在,這意味着一件事 - rspec只是簡單地使用一些緩存版本的文件,因此忽略了我所做的更改。所以即使測試通過了,你也不能確定他們真的這麼做了。這真是奇怪。在用google搜索了一下問題後,我發現如何強制rails來清理緩存 - 請檢查jaustin的答案:is Rails.cache purged between tests?

+0

也強制Guard輪詢可以幫助 - http://stackoverflow.com/questions/11766511/tests-做 - 不運行時,文件的變化,與後衛和-rspec的上窗口 – Nick

相關問題