0

我已经搜索了很多的解决方案,但我找不到它!我想看着迈克尔·哈特尔的教程后,使Rails应用程序,当我手工填写资料并提交什么也没有发生,并没有在邮件程序表记录,我面临着以下错误:水豚和RSpec在新型号规格上的问题

Failures: 

    1) Mailer pages mail us with valid information should send a mail 
    Failure/Error: expect { click_button submit}.to change(Mailer, :count).by(1) 
     count should have been changed by 1, but was changed by 0 
    # ./spec/requests/mailer_pages_spec.rb:31:in `block (4 levels) in <top (required)>' 

Finished in 0.89134 seconds 
2 examples, 1 failure 

Failed examples: 

rspec ./spec/requests/mailer_pages_spec.rb:30 # Mailer pages mail us with valid information should send a mail 

Randomized with seed 17352 

模型文件:

class Mailer < ActiveRecord::Base 
    attr_accessible :company_name, :contact_name, :address, :telephone, :email, :description 
    before_save { |mailer| mailer.email = mailer.email.downcase } 
    validates :company_name, length: { maximum: 50 } 
    validates :contact_name, presence: true, length: { maximum: 40 } 
    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    validates :email, presence: true, format: { with: VALID_EMAIL_REGEX } 
end 

控制器:

class MailersController < ApplicationController 

    def new 
    @mailer = Mailer.new 
    end 

    def create 
    @mailer = Mailer.new(params[:mailer]) 
    if @mailer.save 
     redirect_to root_path 
    else 
     render 'new' 
    end 
    end 
end 

集成测试:

require 'spec_helper' 

describe "Mailer pages" do 

subject { page } 


describe "mail us" do 

    let(:submit) { "Send my Mail"} 
    before { visit mailers_path } 


    describe "with invalid information" do 
     it "should not send a mail" do 
     expect { click_button submit }.not_to change(Mailer, :count) 
     end 
    end 

    describe "with valid information" do 
     before do 
     fill_in "Company name", with: "Mailer Company" 
     fill_in "Contact name", with: "Mailer Contact" 
     fill_in "Address",  with: "Mailer Address" 
     fill_in "Telephone", with: "123-456-789" 
     fill_in "Email",  with: "[email protected].com" 
     fill_in "Description", with: "something to say" 
     end 

     it "should send a mail" do 
     expect { click_button submit}.to change(Mailer, :count).by(1) 
     end 
    end 
    end 
end 

而形式:

<% provide(:title , 'Mail Us') %> 
<h1>Mail Us</h1> 
<div class="row"> 
    <div class="span6 offset3 hero-unit"> 

<%= form_for(@mailer) do |f| %> 
    <%= f.label :company_name %> 
    <%= f.text_field :company_name %> 

    <%= f.label :contact_name %> 
    <%= f.text_field :contact_name %> 

    <%= f.label :address %> 
    <%= f.text_field :address %> 

    <%= f.label :telephone %> 
    <%= f.text_field :telephone %> 

    <%= f.label :email %> 
    <%= f.text_field :email %> 

    <%= f.label :description %> 
    <%= f.text_field :description %> 

    <%= f.submit "Send my Mail", class: "btn btn-large btn-primary"%> 
<% end %> 
</div> 
</div> 

<%=调试(PARAMS)如果Rails.env.development? %>添加到应用程序布局,我看到的行动是新的不创建,是什么造成的问题??等待帮助

+0

当你手工填写并提交它的工作原理? – 2013-05-01 14:27:57

+0

不,我不知道该怎么办 – 2013-05-01 14:29:31

+0

当你尝试时会得到什么错误?添加到你的问题。 – 2013-05-01 14:38:59

回答

0

问题解决了:)我没有添加资源:寄件人在我的路线文件:d

2

您的创建操作指定如果保存该对象,则应将其重定向到root_path,否则应该再次渲染表单(呈现'new')。

因此,如果表单再次呈现,那么这意味着验证失败,并且您的邮件对象尚未保存。以下内容添加到您的形式出现的错误:

<%= form_for(@mailer) do |f| %> 
    <% @mailer.errors.full_messages.each do |msg| %> 
    <p><%= msg %></p> 
    <% end %> 
    ... 
<% end %> 
+0

我试过你的解决方案,但它什么都没有显示:(我该怎么办? – 2013-05-01 14:57:17

+0

注释掉你所有的验证和在Mailer模型中的before_save,重新启动服务器并查看它是否可以正确保存 – 2013-05-01 15:01:40

+0

同样,即使当我设置失败的redirect_to about_path时,它也没有响应这个动作,所以现在怎么样? – 2013-05-01 15:13:06