我有一个Rails应用程序,最近已迁移到Neo4j技术。现在我正在尝试调整或更新现有的测试文件到这项新技术。但是,我遇到了一些困难,很多都失败了。我需要的是理解我将在下面分享的代码。如何更改该代码以适合Neo4j。我需要一个在线参考我找不到任何好的。在具体情况下,我正在共享我理解破坏或删除不起作用,我的错误是预期的结果不匹配。
这是我运行rspec时遇到的错误:
Failure/Error: expect(result).to eq(expected)
expected: {:data=>[{:study_version_id=>nil, :cid=>"X1", :namespace=>"http://www.example.com", :property=>"quest...le.com", :property=>"enabled", :study_value=>"true", :default_value=>"true", :datatype=>"boolean"}]}
got: {:data=>[{:amendment=>{:created_at=>"2017-11-14T13:19:09.000+00:00", :updated_at=>"2017-11-14T13:19:0...true", :default_value=>"true", :datatype=>"boolean", :id=>"fa97a13f-3c2a-4391-80c0-6d61250e7367"}}]}
在我的controller_spec和model_spec下面:
require 'rails_helper'
describe AmendmentsController, type: :controller do
include UserAccountHelpers
include FileHelpers
include JsonHelpers
def sub_dir
return "controllers"
end
describe "Edit Access" do
before :all do
Amendment.delete_all
a = Amendment.create(cid: "X1", namespace: "http://www.example.com", property: "question",
study_value: "ADEF", default_value: "A1", datatype: "string")
a = Amendment.create(cid: "X1", namespace: "http://www.example.com", property: "enabled",
study_value: "true", default_value: "true", datatype: "boolean")
a = Amendment.create(cid: "Y1", namespace: "http://www.example.com", property: "enabled",
study_value: "false", default_value: "true", datatype: "boolean")
ua_create
end
after :all do
Amendment.delete_all
ua_destroy
end
before :each do
login_with_edit_access
end
after :each do
end
it "finds amendments for a given item" do
request.env['HTTP_ACCEPT'] = "application/json"
get :find, {amendment: {cid: "X1", namespace: "http://www.example.com"}}
expect(response.content_type).to eq("application/json")
expect(response.code).to eq("200")
result = remove_id_and_timestamps(response)
#write_yaml_file(result, sub_dir, "amendments_controller_find_1.txt")
expected = read_yaml_file(sub_dir, "amendments_controller_find_1.txt")
expect(result).to eq(expected)
end
it "finds amendments for a given item, none present" do
request.env['HTTP_ACCEPT'] = "application/json"
get :find, {amendment: {cid: "X11", namespace: "http://www.example.com"}}
expect(response.content_type).to eq("application/json")
expect(response.code).to eq("200")
#write_text_file(response.body, sub_dir, "amendments_controller_find_2.txt")
expected = read_text_file(sub_dir, "amendments_controller_find_2.txt")
expect(response.body).to eq(expected)
end
it "creates an amendment" do
request.env['HTTP_ACCEPT'] = "application/json"
post :create, {amendment: {cid: "X11", namespace: "http://www.example.com", property: "question",
study_value: "SSS", default_value: "AAA", datatype: "string"}}
expect(response.content_type).to eq("application/json")
expect(response.code).to eq("200")
end
it "updates an amendment" do
a = Amendment.create(cid: "X11", namespace: "http://www.example.com", property: "question",
study_value: "ADEF", default_value: "A1", datatype: "string")
request.env['HTTP_ACCEPT'] = "application/json"
put :update, {id: a.id, amendment: {property: "question",
study_value: "SSS", default_value: "AAA", datatype: "string"}}
expect(response.content_type).to eq("application/json")
expect(response.code).to eq("200")
#write_text_file(response.body, sub_dir, "amendments_controller_create_1.txt")
expected = read_text_file(sub_dir, "amendments_controller_create_1.txt")
expect(response.body).to eq(expected)
end
it "destroys an amendment" do
a = Amendment.create(cid: "X11", namespace: "http://www.example.com", property: "question",
study_value: "ADEF", default_value: "A1", datatype: "string")
request.env['HTTP_ACCEPT'] = "application/json"
delete :destroy, {id: a.id}
expect(response.content_type).to eq("application/json")
expect(response.code).to eq("200")
end
end
describe "Unauthorized User" do
it "find" do
get :find
expect(response).to redirect_to("/users/sign_in")
end
it "create" do
post :create
expect(response).to redirect_to("/users/sign_in")
end
it 'update' do
put :update, {id: 1}
expect(response).to redirect_to("/users/sign_in")
end
it 'destroy' do
delete :destroy, {id: 1}
expect(response).to redirect_to("/users/sign_in")
end
end
end
# MODEL
require 'rails_helper'
describe Amendment do
before :each do
Amendment.(&:destroy)
end
after :each do
Amendment.(&:destroy)
end
it "serializes the amendmemt as a hash" do
amendment = Amendment.create(cid: "XXX", namespace: "http://ww.example.com", property: "question",
study_value: "ADEF", default_value: "A", datatype: "string")
result = amendment.to_hash
expected =
{
cid: "XXX",
namespace: "http://ww.example.com",
property: "question",
study_value: "ADEF",
default_value: "A",
datatype: "string"
}
expect(result).to eq(expected)
end
end