2011-06-06 73 views
1

我測試了我的Rails應用程序的視圖層。如何使用RSpec模擬出現錯誤的模型

這種觀點是對客戶的模式,在那裏我打電話的錯誤吧,像這樣(HAML):

- if @customer.errors.present? 
    - flash[:warn] = "" 
- @customer.errors.full_messages.each do |msg| 
    - flash[:warn] += msg + "<br />" 

= form_for :customer, :url => customer_registration_path do |f| 
    - validated = resource.errors.any? 
    .field{ :class => !validated ? "" : (resource.errors.has_key?(:email) ? "failed" : "passed") } 
    = f.text_field "email", :placeholder => "почта", :autocomplete => "off" 
    .status 
    .field{ :class => !validated ? "" : (resource.errors.has_key?(:password) ? "failed" : "passed") } 
    = f.password_field "password", :placeholder => "пароль", :autocomplete => "off" 
    .status 
    .field{ :class => !validated ? "" : (resource.errors.has_key?(:password_confirmation) ? "failed" : "passed") } 
    = f.password_field "password_confirmation", :placeholder => "повторите пароль", :autocomplete => "off" 
    .status 
    = f.submit "" # Зарегистрироваться 

我需要測試的,也就是說存在,來包裹「電子郵件」欄中,標明作爲「失敗」。

如何爲客戶模型編寫模擬模擬相應錯誤的存在?

+0

我認爲這是重複:https://stackoverflow.com/questions/ 6248335 /如何對模擬模型與 - 錯誤 - 使用 - rspec的 – 2014-11-14 02:49:16

回答

0

我的做法是製作實際無效的物體,而不是真正的嘲笑。 但是,如果你堅持:

it "should render errors" do 
    @user = User.new 
    @user[:errors][:email] = 'invalid' 
    @view.stub!(:resource).and_return { @user } 
    render 
    ...... 
end 

順便說一句,我想大多數的你打造是由動態的形式提供的功能:https://github.com/joelmoss/dynamic_form