0

导轨 3.2.6

我已经按照本文的建议设置了一个联系人控制器和表单:http: //matharvard.ca/posts/2011/aug/22/contact-form-in-rails-3/

我的Message模型课:

class Message
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  attr_accessor :name, :email, :message_body

  # validations are here...

  def initialize(attributes = {})
      attributes.each do |name, value|
      send("#{name}=", value)
      end
  end

  def persisted?
      false
  end
end

Contact Controller:_

def create
    @message = Message.new(params[:message])

    if @message.valid?
        ContactMailer.new_message(@message).deliver
        redirect_to(root_path, :notice => "Message was successfully sent.")
    else
        render :new
    end
end

我的 rspec 控制器测试:

describe "POST create" do
    context "with valid information" do
      let (:message) { FactoryGirl.build(:message) }
      it "creates a new message" do
        post :create, params: message
        assigns(:message).should eq(message)
    end
  end
end

我已经验证了message变量 vialet不是 nil,所以 FactoryGirl 正在做它的工作。

测试结果:

ContactController POST create with valid information creates a new message
 Failure/Error: post :create, params: message
 NoMethodError:
   undefined method `each' for nil:NilClass
 # ./app/models/message.rb:14:in `initialize'
 # ./app/controllers/contact_controller.rb:8:in `new'
 # ./app/controllers/contact_controller.rb:8:in `create'
 # ./spec/controllers/contact_controller_spec.rb:22:in `block (4 levels) in <top (required)>'

我知道它失败是因为Message'initialize方法。但我现在确定它为什么初始化 nil。当我在开发环境中测试表单时,它似乎工作正常。谢谢你的帮助。

4

2 回答 2

0

我认为您应该使用明确的名称传递消息,如下所示:

it "creates a new message" do
    post :create, :message => message
    assigns(:message).should eq(message)
end

或短样式:

post :create, message: message

那么你将'message'在 params hash 中有一个键

params[:message] = ... # here's your message

否则你将没有钥匙,params因此params[:message]回报为零

于 2012-08-17T00:36:04.177 回答
0

我必须将哈希值作为值传递给post's hash 选项。像这样:

message_attributes = FactoryGirl.attributes_for(:message)
post :create, message: message_attributes

为了比较测试中的两条消息,我不得不Message像这样修改模型:

attr_accessor :attributes, :name, :email, :message_body
def initialize(attributes = {})
    if not attributes.nil?
        attributes.each do |name, value|
            send("#{name}=", value)
        end
    end
    @attributes = attributes
end

所以最终的通过测试看起来像这样:

it "creates a new message" do
    message_attributes = FactoryGirl.attributes_for(:message)
    post :create, message: message_attributes
    assigns(:message).attributes.symbolize_keys.should eq(message_attributes)
end

我不确定这是否是最好的解决方案,但它确实创建了一个通过测试并验证控制器是否正确创建了一条消息。

于 2012-08-17T20:14:09.717 回答