显然我对rails很陌生,所以坚持我。
我在我的模型中添加了一个构造函数
class Movie < Media
attr_accessible :director, :studio
attr_accessor :director, :studio
validates_presence_of :director, :studio, :title
def initialize title, director, studio
@title = title
@director = director
@studio = studio
end
end
和那种对我来说搞砸的事情。在我的控制器中有一个像这样的“新”方法之前
def new
@movies = Movie.new
end
在初始化出现之前它运行良好。它需要将参数传递给“新”方法,但这是在打开视图以从用户传递参数并保存它们之后完成的。现在我无法打开该视图,因为出现错误
wrong number of arguments (0 for 3)
由于我开始为我的应用程序编写测试并为构造函数设置默认值将使该测试无效,因此添加了构造函数。关于解决这个问题的建议?
编辑:我的测试如下所示:
require 'spec_helper'
describe Movie do
before :each do
@movie = Movie.new "Bullet", "John", "20th"
end
describe "#{new}" do
it "returns new object of Movie" do
@movie.should be_an_instance_of Movie
end
it "throws ArgumentError when give less than 3 parameters" do
lambda {Movie.new(:director => "John", :studio => "20th")}.should raise_exception ArgumentError
end
end
describe "#title" do
it "returns the correct title" do
@movie.title.should eql "Bullet"
end
end
describe "#director" do
it "returns the correct director" do
@movie.director.should eql "John"
end
end
describe "#studio" do
it "returns the correct studio" do
@movie.studio.should eql "20th"
end
end
end
没有那个构造函数,所有的测试都会失败......