0
我有一個目錄控制器和一個文件控制器。我正在測試文件控制器。我已經爲該文件創建了有效的屬性,我試圖mock_model目錄來讓測試通過。 GET測試全部正常,但POST測試均無效。 POST測試都給出了錯誤:「預期的目錄,得到了String。」rails rspec mock_model預期的對象,得到的字符串
describe FilesController do
def valid_attributes {
:name => "test",
:reference_id => 1,
:location => "/path/to/directory",
:software => "excel",
:software_version => "2010",
:directory => mock_model(Directory)
}
end
describe "POST create" do
describe "with valid params" do
it "creates a new AssemblyFile" do
expect {
post :create, :assembly_file => valid_attributes
}.to change(AssemblyFile, :count).by(1)
end
it "assigns a newly created assembly_file as @assembly_file" do
post :create, :assembly_file => valid_attributes
assigns(:assembly_file).should be_a(AssemblyFile)
assigns(:assembly_file).should be_persisted
end
it "redirects to the created assembly_file" do
post :create, :assembly_file => valid_attributes
response.should redirect_to(AssemblyFile.last)
end
end
end
1) FilesController POST create with valid params creates a new File
Failure/Error: post :create, :file => valid_attributes
ActiveRecord::AssociationTypeMismatch:
Directory(#87017560) expected, got String(#49965220)
# ./app/controllers/files_controller.rb:60:in `new'
# ./app/controllers/files_controller.rb:60:in `create'
# ./spec/controllers/files_controller_spec.rb:79:in `block (5 levels) in <top (required)>'
# ./spec/controllers/files_controller_spec.rb:78:in `block (4 levels) in <top (required)>'
如果我看test.log中文件,它表明組件是一個字符串( 「組件」=> 「1011」)。所以我不確定爲什麼mock_model沒有創建一個對象?
我使用存根試過!而不是mock_model,但由於創建而變得複雜!用於存根!需要很多自己的有效變量集,而且我根本不想爲那些甚至根本不測試目錄控制器的人設置一大堆其他有效屬性。
我在做什麼錯在這裏我的方法?