[英]class method_1 doesn't pass the test, but it works in rails console
我有以下兩種模型:
class Project < ActiveRecord::Base
has_many :tasks, inverse_of: :project
attr_accessible :tasks_attributes
def method_1
tasks.map |t|
t.name
end
end
class Task < ActiveRecord::Base
belongs_to :project, inverse_of: :tasks
attr_accessible :name
validates :name, presence: true
end
我正在為project_spec.rb編寫測試。 我測試關聯和驗證,它們都很好。 但以某種方式我無法通過method_1的測試。 我更改了method_1代碼,如下所示,只是為了進行檢查。 我得到的結果是[nil],即task.first是nil。 我不知道我在想什么。
def method_1
[tasks.first]
end
我的測試代碼是
...
let(:project) do
build_stubbed(:project, :project_data)
end
describe "#method_1" do
before do
project.stub_chain(:task, :name).and_return("task A")
end
subject {project.method_1}
it "should be the name of the task of the project" do
should eq ["task A"]
end
end
當我運行localhost:3000時,它有同樣的問題。 但在Rails控制台中,method_1也可以正常工作。 有人可以幫忙嗎? 它困擾了我好幾天。
以下是一些可能有幫助的評論:
method_1
依賴於tasks
,但是您已將task
存根(單數),因此它沒有任何作用。 method_1
在Rails控制台中有效,因為您在開發數據庫中有一個與項目相關的任務
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.