这是我的验证测试,我想找到编写模型规范的最佳方法,特别是验证。但我对下面的代码有问题。
require 'spec_helper'
describe Ad, :focus do
let(:ad) { Ad.sham!(:build) }
specify { ad.should be_valid }
it "not creates a new instane given a invalid attribute" do
ad = Ad.new
ad.should_not be_valid
end
[:title, :category_id, :email, :ad_content, :name, :price].each do |attr|
it "should require a #{attr}" do
subject.errors[attr].should include("blank")
end
end
end
Run Code Online (Sandbox Code Playgroud)
当我运行此规范时,我收到此错误:
5) Ad should require a name
Failure/Error: subject.errors[attr].should include("blank")
expected [] to include "blank"
Diff:
@@ -1,2 +1,2 @@
-blank
+[]
# ./spec/model/ad_spec.rb:15:in `block (3 levels) in …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
Group.where('name ~* ?', params[:name]).first
Run Code Online (Sandbox Code Playgroud)
where在那种情况下如何存根方法?
Group.stub(:where).and_return(mock_model(Group, name: "SomeName"))
Run Code Online (Sandbox Code Playgroud)
导致错误:
Mock "Group_1001" received unexpected message :first with (no args)
Run Code Online (Sandbox Code Playgroud) 我尝试在控制器中添加assigns我的update动作规范
it "update the content" do
Answer.should_receive(:find).with(answer.id.to_s).and_return(answer)
answer.should_receive(:update_attributes).with("content" => "Changed content")
put :update, id: answer.id, app_id: app.id, answer: {content: "Changed content"}
assigns(:answer).content.should eq('Changed content') # explicitly permitted
response.status.should eq 406
end
Run Code Online (Sandbox Code Playgroud)
但我得到错误:
Failure/Error: assigns(:answer).content.should eq('Changed content') # explicitly permitted
expected: "Changed content"
got: "base content"
(compared using ==)
Run Code Online (Sandbox Code Playgroud)
但是当我评论时:
#answer.should_receive(:update_attributes).with("content" => "Changed content")
规格通过.为什么?
当我在rails应用程序文件夹中保存文件冻结约5秒时会出现什么问题?如何调查此问题?
VIMRC:https://gist.github.com/4632cbcfc655899c934f